VBA - Using Declared Range Variable in looping script across multiple sheets

Lambrix

Board Regular
Joined
Jun 29, 2012
Messages
62
I am trying to adjust the code so that it cycles through the sheets using a range variable rather than hard coding Range ("O4"). However, when I run the code, it works on the first sheet and then gives me a run-time error '1004' when it gets to "MyRange.Select" - Any ideas what I am doing wrong here? :confused::banghead::banghead:


Code:
        Sub AllWorkSheets()


        Dim ActSheet As Worksheet
        Dim Junction As String
        Dim BOC As String
        Dim Amount As String
        Dim MyRange As Range
        Set MyRange = ActiveCell

        '' Added the MyRange Variable to replace the hard coded Values below used in Amount and Range

        Sheets("Aggregate").Activate
        Set ActSheet = ActiveSheet
        Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets

        If Left(ws.Name, 8) = "Junction" Or Left(ws.Name, 8) = "National" Then
        ws.Select

        ' Blocked and copied code while I test
        'Amount = Range("O4").Value
        'Range("O4").Activate
        'Amount = Range("O4").Value

        Amount = MyRange.Value
        MyRange.Select
        Amount = MyRange.Value

        BOC = ActiveCell.Offset(0, -12).Value
        Junction = ActiveCell.Offset(0, -14).Value
        ActSheet.Select
        Range("B1").Value = "BOCs"
        Range("A1").Value = "Junction Num"
        Cells(Rows.Count, 1).End(xlUp).Select
        ActiveCell.Offset(1, 0).Range("A1").Select
        ActiveCell.Value = Junction
        ActiveCell.Offset(0, 2) = Amount
        ActiveCell.Offset(0, 1) = BOC

        End If
        Next

        End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You are changing the active sheet with your ws.Select line so MyRange is no longer on the active sheet and therefore cannot be selected. You don't need all the selecting and activating - it just slows the execution down.
 
Upvote 0
You are changing the active sheet with your ws.Select line so MyRange is no longer on the active sheet and therefore cannot be selected. You don't need all the selecting and activating - it just slows the execution down.

Thanks for your response Joe.

All the sheets are structured exactly the same way. I am trying to set a range variable based on my current active cell, and then use the range variable to pull that information using a loop on the remaining worksheets.

If the Range Variable is only valid on the activesheet, How would I grab this information on the remaining worksheets?
 
Upvote 0
Move the Set MyRange = Activecell into the For each ws loop.
 
Upvote 0
Move the Set MyRange = Activecell into the For each ws loop.

It stopped the error message but does not grab the correct range on the other sheets once it starts looping. For example, if I select cell "O5" on my active sheet, I would want to grab the information in "O5" on all the sheets.

How would I go about passing the correct range variable across all the worksheets? Would I need to use a public range variable? What do you suggest?

Thanks again.
 
Upvote 0
Maybe:
Dim MyRangeAdr as string, MyRange as Range
Set MyRange = ActiveCell
MyRangeAdr = MyRange.Address(0,0)

Then inside your For Each Loop
set MyRange = ws.Range(MyRangeAdr)
 
Upvote 0
You could put the address into a string.
Code:
Dim sRngAddress as String
sRngAddress = Activecell.Address
Sheets("Whatever").Range(sRngAddress) = "Yipee"
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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