Loop through Sheets to get single value

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi guys,
I have been browsing through the threads in Mr. Excel and can't quite find what I am looking for.
I have a workbook with >200 sheets that I would like to loop through each one and grab a single value to put into a summary sheet at the front of it all.

Something like below,
Book1
ABCDEFGHIJK
1WksheetValueSection 1ASection 2ASection 3ASection 4ASection 5ASection 6ASection 7A
2Section 1A-0.75Value in $B$4=-0.75-0.710.52-0.220.830.010.04
3Section 2A-0.71
4Section 3A0.52
5Section 4A-0.22
6Section 5A0.83
7Section 6A0.01
8Section 7A0.04
9Summary WorksheetIndividual Worksheets
Sheet1
VBA Code:


I have tried a few things, but can't quite get the result I am looking for.
Thinking something like this might work at a simple level, but with so many sheets, its hard to know if all the names/values correctly match up.
Any ideas are greatly appreciated! Thanks

VBA Code:
Sub CollateData()

    Dim ws As Worksheet
    Dim i As Integer

i = 1
    For Each ws In Worksheets
        If ws.Name <> "Summary Sheet" Then
           ws.range("$B$4").Copy Destination:=Sheets("Summary Sheet").Cells(B,i)
        End If
i = i +1
    Next

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi guys,
I have been browsing through the threads in Mr. Excel and can't quite find what I am looking for.
I have a workbook with >200 sheets that I would like to loop through each one and grab a single value to put into a summary sheet at the front of it all.

Something like below,
Book1
ABCDEFGHIJK
1WksheetValueSection 1ASection 2ASection 3ASection 4ASection 5ASection 6ASection 7A
2Section 1A-0.75Value in $B$4=-0.75-0.710.52-0.220.830.010.04
3Section 2A-0.71
4Section 3A0.52
5Section 4A-0.22
6Section 5A0.83
7Section 6A0.01
8Section 7A0.04
9Summary WorksheetIndividual Worksheets
Sheet1
VBA Code:


I have tried a few things, but can't quite get the result I am looking for.
Thinking something like this might work at a simple level, but with so many sheets, its hard to know if all the names/values correctly match up.
Any ideas are greatly appreciated! Thanks

VBA Code:
Sub CollateData()

    Dim ws As Worksheet
    Dim i As Integer

i = 1
    For Each ws In Worksheets
        If ws.Name <> "Summary Sheet" Then
           ws.range("$B$4").Copy Destination:=Sheets("Summary Sheet").Cells(B,i)
        End If
i = i +1
    Next

End Sub
Was thinking that maybe instead of copy and pasting the value, it would be better to copy and paste the reference to the formula, then when values in each sheet change the summary table is up to date without a macro running.
 
Upvote 0
give this a try
VBA Code:
Sub CollateData()

    Dim ws As Worksheet
    Dim i As Integer
    Dim dict As Object
   
Set dict = CreateObject("Scripting.Dictionary")

For i = 1 To Worksheets.Count
    Set ws = Sheets(i)
    If ws.Name <> "Summary Sheet" Then
        dict.Add ws.Name, ws.Range("B4").Value
    End If
Next i

Sheets("Summary Sheet").Range("B2").Resize(dict.Count, 2) = Application.Transpose(Array(dict.keys, dict.items))

End Sub
 
Last edited:
Upvote 0
@NoSparks ;)

VBA Code:
Sheets("Summary Sheet").Range("B2").Resize(dict.Count, 2) = Application.Transpose(Array(dict.keys, dict.items))
 
Upvote 0
give this a try
VBA Code:
Sub CollateData()

    Dim ws As Worksheet
    Dim i As Integer
    Dim dict As Object
  
Set dict = CreateObject("Scripting.Dictionary")

For i = 1 To Worksheets.Count
    Set ws = Sheets(i)
    If ws.Name <> "Summary Sheet" Then
        dict.Add ws.Name, ws.Range("B4").Value
    End If
Next i

Sheets("Summary Sheet").Range("B2").Resize(dict.Count, 2) = Application.Transpose(Array(dict.keys, dict.items))

End Sub
Thank you NoSparks, for getting the value this works perfectly.

One question, is there a way to use the same thing to copy the formula from the worksheets into the summary sheet so they would stay live/dynamic when the value in B4 changed?
 
Upvote 0
Thank you NoSparks, for getting the value this works perfectly.

One question, is there a way to use the same thing to copy the formula from the worksheets into the summary sheet so they would stay live/dynamic when the value in B4 changed
sorry, a little slow on the update. Changing the .value to .formula does that. Thank you NoSparks!
 
Upvote 0
sorry, a little slow on the update. Changing the .value to .formula does that. Thank you NoSparks!
Except it doesn't bring the sheet name with it so the formula doesn't work. Can i CONCAT with dict.items the ws.Name somehow to get the summary sheet to carry the full formula?
 
Upvote 0
try like this
VBA Code:
Sub CollateData()

    Dim ws As Worksheet
    Dim i As Integer
    Dim dict As Object
 
Set dict = CreateObject("Scripting.Dictionary")

For i = 1 To Worksheets.Count
    Set ws = Sheets(i)
    If ws.Name <> "Summary Sheet" Then
        dict.Add ws.Name, "=" & ws.Name & "!B4"
    End If
Next i

'Sheets("Summary Sheet").Range("B2").Resize(dict.Count, 2) = Application.Transpose(Array(dict.keys, dict.items))

' same thing as 2 separate instructions
With Sheets("Summary Sheet")
    .Range("B2").Resize(dict.Count) = Application.Transpose(dict.keys)
    .Range("C2").Resize(dict.Count) = Application.Transpose(dict.items)
End With

End Sub
 
Last edited:
Upvote 0
Solution
try like this
VBA Code:
Sub CollateData()

    Dim ws As Worksheet
    Dim i As Integer
    Dim dict As Object
 
Set dict = CreateObject("Scripting.Dictionary")

For i = 1 To Worksheets.Count
    Set ws = Sheets(i)
    If ws.Name <> "Summary Sheet" Then
        dict.Add ws.Name, "=" & ws.Name & "!B4"
    End If
Next i

'Sheets("Summary Sheet").Range("B2").Resize(dict.Count, 2) = Application.Transpose(Array(dict.keys, dict.items))

' same thing as 2 separate instructions
With Sheets("Summary Sheet")
    .Range("B2").Resize(dict.Count) = Application.Transpose(dict.keys)
    .Range("C2").Resize(dict.Count) = Application.Transpose(dict.items)
End With

End Sub
This does copy the formula so it works more or less getting the formula and sheet reference into the summary cell, but it has a strange thing it does when I run it. It opens a window to "Update Values:" and is looking for a filename.

1640119233244.png
 
Upvote 0
Works fine for me and I can't re-create what you're describing so I have no idea, sorry
 
Upvote 0

Forum statistics

Threads
1,223,054
Messages
6,169,834
Members
452,284
Latest member
TKM623

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