Loop through of sheets and execute calculations based on dynamic sheet references

suremac

New Member
Joined
Jan 27, 2014
Messages
49
Greetings,


Is it possible to loop through a list of sheets and execute some calculations that have sheet references from a different list of worksheets? For example, you have a list (list 1) of your worksheets, which will be the destinations of the calculations, and you have a second list (list 2) of worksheets that the calculations are based on. So, lets say there are sheet1 and sheet2 in list 1, and sheetA and sheetB in list 2. The calculations based on sheetA would appear in sheet1, and calculations based on sheetB would appear in sheet2. I thought the code would look something like this:


Code:
Sub LoopthroughWorksheets()
    Dim sheet_name As Range
    Dim sheet_name2 As Range
    Set sheet_name2 = Sheets("WS").Range("F:F")
    For Each sheet_name In Sheets("WS").Range("C:C")
        If sheet_name.Value = "" Then
            Exit For
        Else
            With Sheets(sheet_name.Value)
                    .Range("K1") = .Range("sheet_name2.Value!A14").Value
            End With
        End If
        Next sheet_name
    End Sub


I'm getting a "Run-time error '1004: Application-defined or object defined error" at this line:
Code:
.Range("K1") = .Range("sheet_name2.Value!A14").Value


Any help is much appreciated.


Regards,
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I believe this is what you want:
Code:
.Range("K1").Value = Worksheets("sheet_name2").Range("A14").Value
 
Upvote 0
Thanks for the help pbornemeier. This is the code I was trying to produce.

Code:
Sub LoopthroughWorksheets()
    Dim sheet_name As Range
    Dim sheet_name2 As Range
    Set sheet_name2 = Sheets("WS").Range("F:F")
    ' NEW
    Dim counter As Long
    counter = 1


    For Each sheet_name In Sheets("WS").Range("C:C")
        If sheet_name.Value = "" Then
            Exit For
        Else
            With Sheets(sheet_name.Value)
                .Range("K1").Value = Sheets(sheet_name2(counter, 1).Value).Range("A14").Value
                ' NEW                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                   
                counter = counter + 1
            End With
        End If
    Next sheet_name
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,365
Messages
6,124,513
Members
449,168
Latest member
CheerfulWalker

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