parry
MrExcel MVP
- Joined
- Aug 20, 2002
- Messages
- 3,355
Hi, I am having trouble understanding ranges in VBA.
I have the following code where I am selecting cells a1:a3 first by using a string to define the range then a range type. The string works fine but the range type comes up with runtime error 1004:'Method Range of Object'_Global Fail. Clicking on Help shows no topic grrrrr.
Could someone please explain how you are supposed to declare then use range variables. Im stuck and searching VB help showed nothing useful.
I have the following code where I am selecting cells a1:a3 first by using a string to define the range then a range type. The string works fine but the range type comes up with runtime error 1004:'Method Range of Object'_Global Fail. Clicking on Help shows no topic grrrrr.
Could someone please explain how you are supposed to declare then use range variables. Im stuck and searching VB help showed nothing useful.
Code:
Sub test()
Dim rngtest As Range
Dim strtest As String
Set rngtest = Range("a1", "a3")
strtest = "a1:a3"
Range(strtest).Select ' works
Range(rngtest).Select ' runtime error 1004
End Sub