Use a variable for multiple ranges in a With/End With statement

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
Trying to save multiple ranges to a variable.

VBA Code:
Sub WorkingTest()
'
    With Range("B10:B19", "C10:C19")
        .Value = Evaluate("Row(" & .Address & ") - 9")      ' Prints 1 thru 10 in each range
    End With
End Sub

When I try to use a variable of the saved ranges, I get #VALUE!

VBA Code:
Sub NotWorking()
'
    String1 = "B10:B19"
    String2 = "C10:C19"
    String3 = String1 & ", " & String2
'
    With Range(String3)
        .Value = Evaluate("Row(" & .Address & ") - 9")
    End With
End Sub

Any suggestions how to fix what I am doing wrong?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Thank you @Tetra201. That does work, however it will not work for the code that I will be using. :(

The code I will be using searches for columns that qualify. When a column is deemed to qualify, the range of that column is saved. The actual amount of qualifying columns is unknown until the code is ran. That is why I was asking for an approach that would utilize one variable for all of the added column ranges.
 
Upvote 0
There is nothing wrong with using Range(String3). It's the ROW function that throws the error -- its argument cannot refer to multiple areas.

Was the ROW function used just for testing purposes? -- Or you will need to use it in your actual code?

Update: here is how to modify your NotWorking sub:
VBA Code:
Sub NotWorking()
'
    String1 = "B10:B19"
    String2 = "C10:C19"
    String3 = String1 & ", " & String2
'
    For Each c In Range(String3).Areas
        c.Value = Evaluate("Row(" & c.Address & ") - 9")
    Next c
End Sub
 
Last edited:
Upvote 0
Solution
There is nothing wrong with using Range(String3). It's the ROW function that throws the error -- its argument cannot refer to multiple areas.

Was the ROW function used just for testing purposes? -- Or you will need to use it in your actual code?
Spot on @Tetra201!

That was just testing code. When I switched to the actual code, it worked!!!

VBA Code:
    With WS.Range(String3))
        .Replace What:="", Replacement:="*", LookAt:=xlPart                                     ' Add '*' to all of the blank cells in the column ranges saved
    End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top