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
 
Works fine for me and I can't re-create what you're describing so I have no idea, sorry
Thanks for checking it on your side and for sharing the solution in the first place. I will trouble-shoot mine to get it working.
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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
I found the issue, small edit to your original code adding ' into the dictionary item for the worksheet name. Without the ' , my version of excel O365 was creating external links (even thought same workbook) and these links were being asked to be updated each time the CollateData macro ran. Everything working now! Thanks NoSparks.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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