PDA

View Full Version : Assign Value to Variable Type Range



nepotist
03-29-2011, 10:20 AM
Hello,
I have a function that has a argument of type Range. I am trying to check for value of this argument before I could do some calculations.

Function Test (Rn as Range)
If (Rn = 'T") then Rn ="Test" 'I have even tried Rn.Value
End Function


My code doesn't return an error nor it assigns the Rn the Test Value. How to work with assigning values to Ranges?

nepotist
03-29-2011, 10:34 AM
I used a variable to validate the range value and it worked. Currently I am directly using the range (Rn) in my calculations. Should I be using Rn.Value? whats the difference adn how does it affect?

mdmackillop
03-29-2011, 11:35 AM
Your T has typo in the quotes

Sub TestIt()
Test Cells(1, 1)
End Sub


Function Test(Rn As Range)
If Rn = "T" Then Rn = "Test"
End Function

Kenneth Hobs
03-29-2011, 11:48 AM
I think it good programming technique to specify the property for the Range object. While Value is the default property for a Range object, Value2 is more efficient if it does not have a type of Currency or Date.

You could just use bracket's, []'s, (e.g. [A1]) for the Range and value. I don't like Cells() all that much either. While it is sometimes more efficient than Range(), intellisense will not give you the methods and properties for the Range object for the Cells() or [] methods. For dates, I still like Range's Value2 property to return a number rather than the formatted date sometimes.

nepotist
03-29-2011, 05:29 PM
Cool Thanks!!