1

I'm trying to select two different ranges in a loop (i.e. first iteration selects one range, second iteration selects another range). Here's my try at just seeing if I could do a simple task like selecting two ranges one after another:

Sub SelectingTwoRanges()
Dim i As Integer
Dim m As Integer
Dim n As Integer
For i = 1 To 2
    m = i * 50 - 48
    n = i * 50 + 1
    Range(Cells(m, 1), Cells(n, 2)).Select
Next i

End Sub

This gives the error: "Method 'Cells' of Object '_Global' Failed"

I've tried it before as:

Sub SelectingTwoRanges()
Dim i As Integer
Dim m As Integer
Dim n As Integer
For i = 1 To 2
    m = i * 50 - 48
    n = i * 50 + 1
    Range("Am:Bn").Select
Next i

End Sub

Or:

Sub SelectingTwoRanges()
Dim i As Integer
Dim m As Integer
Dim n As Integer
For i = 1 To 2
    m = i * 50 - 48
    n = i * 50 + 1
    Range("A$n:B:m").Select
Next i

End Sub

And none of the above worked. I think my problem is using loop-dependent variable inside strings (which I tried to avoid by using the "Cells(#, #)..." formation inside the "Range()". But now I'm just stuck.

Is this possible?

5
  • the provided example does work fine, is there any other code that might interfere with it? for your second example, the code should look like Range("A" & m & ":B" & n).Select , this should work as well. Jun 26, 2014 at 16:26
  • Your first sample worked perfectly for me.
    – tbur
    Jun 26, 2014 at 16:27
  • try fully qualifying your range reference(s) with a sheet name and / or sheet number.
    – sous2817
    Jun 26, 2014 at 16:31
  • The second and third example use quotes the wrong way in the range address. They should be like Range("A" & n & ":B" & m).Select.
    – tbur
    Jun 26, 2014 at 16:34
  • are you using merged cells ? or locked sheet/cells,? or hidden ? (can't select those...) Jun 27, 2014 at 16:15

2 Answers 2

2

It's likely you're getting the "Method 'Cells' of Object '_Global' Failed" error because your's Cells() method isn't attached to an object. According to this post (Method 'Range' of object '_Global' failed. error), you can avoid that by using "activeSheet".

Sub SelectingTwoRanges2()
    Dim i As Integer
    Dim m As Integer
    Dim n As Integer
    For i = 1 To 2
        m = i * 50 - 48
        n = i * 50 + 1
        With ActiveSheet
            .Range(.Cells(m, 1), .Cells(n, 2)).Select
        End With
    Next i
End Sub

As for your other examples; you're completely right. Including a variable name in a string doesn't reference the variable at all. You can use the following format to concatenate strings (VBA will automatically try to convert variable values to strings in concatenation)

Range("A" & m & ":B" & n).Select 

Personally I like using CStr() to make sure VBA converts a value to a string

Range("A" & CStr(m) & ":B" & CStr(n)).Select
1

The first example worked for me, but try:

 activesheet.Range(activesheet.Cells(m, 1), activesheet.Cells(n, 2)).Select

This will fully qualify the Cells() range reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.