Macro Summary Creation

smn

New Member
Joined
Apr 25, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to develop a macro that will help me summarize information across several sheets in a workbook over a certain (changeable) date range for a number of unique fields.


So the summary sheet will look something like the below. The user will be able to input two dates and then run the macro to see the sum of values across the workbook for those dates for each of the letters A, B, C, D, etc.

1682402814586.png


The data sheets are identical apart from their names and will look like the below (named "S1"). There will be the weeks as headers (x1, x2, x3, x4, x5, etc.). The unique fields (A, B, C, D) are columns. Each unique field is not present in every sheet (i.e., this one only has B and F).

1682402924031.png


If I had a second worksheet (named "S2") as below, then ideally the after running would display the following result:
1682403056791.png



1682403211516.png


Would this type of macro be possible? And if so, how could it be accomplished? Any help is greatly appreciated!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Yes, it is possible. What are the real names of the sheets? I will look if a pattern is possible to loop through them. Otherwise, the sheetnames should be hardcoded into a names array.

Also I will need the weeks row number on each, items column letter on each sheet, dates rows and columns on results sheet.
 
Upvote 0
Yes, it is possible. What are the real names of the sheets? I will look if a pattern is possible to loop through them. Otherwise, the sheetnames should be hardcoded into a names array.

Also I will need the weeks row number on each, items column letter on each sheet, dates rows and columns on results sheet.
Hi, there will be many sheet names whose names are listed on a separate sheet (from A7-A40). The sheet names are not related to each other, so there will not be a discernable pattern.

The weeks row number will be always be '4'. The items column letter on each sheet will always be 'B.' On the results sheet, the Date1 will be B2 and Date2 will be B3.
 
Upvote 0
This should work (y) I am also sharing the sample file :)
VBA Code:
Sub test()
  Dim results As Worksheet, wsNames As Variant
  Set results = Worksheets("Results")
  wsNames = Split("S1,S2", ",")
  results.Range("B7:B" & results.Cells(Rows.Count, "A").End(xlUp).Row) = 0
  For i = 7 To results.Cells(Rows.Count, "A").End(xlUp).Row
    For Each ws In wsNames
      With Worksheets(ws)
        For r = 5 To .Cells(Rows.Count, "B").End(xlUp).Row
          If .Cells(r, "B").Value2 = results.Cells(i, "A").Value2 Then
            For c = 3 To .Cells(4, Columns.Count).End(xlToLeft).Column
              If .Cells(4, c).Value2 >= results.Range("B2").Value2 And .Cells(4, c).Value2 <= results.Range("B3").Value2 Then
                results.Cells(i, "B").Value2 = results.Cells(i, "B").Value2 + .Cells(r, c).Value2
              End If
            Next
            GoTo nextWS 'Delete this line if a letter appears more than once in a column
          End If
        Next
      End With
nextWS: 'Delete this line if a letter appears more than once in a column
    Next
  Next
End Sub

 
  • Like
Reactions: smn
Upvote 0
Solution
This should work (y) I am also sharing the sample file :)
VBA Code:
Sub test()
  Dim results As Worksheet, wsNames As Variant
  Set results = Worksheets("Results")
  wsNames = Split("S1,S2", ",")
  results.Range("B7:B" & results.Cells(Rows.Count, "A").End(xlUp).Row) = 0
  For i = 7 To results.Cells(Rows.Count, "A").End(xlUp).Row
    For Each ws In wsNames
      With Worksheets(ws)
        For r = 5 To .Cells(Rows.Count, "B").End(xlUp).Row
          If .Cells(r, "B").Value2 = results.Cells(i, "A").Value2 Then
            For c = 3 To .Cells(4, Columns.Count).End(xlToLeft).Column
              If .Cells(4, c).Value2 >= results.Range("B2").Value2 And .Cells(4, c).Value2 <= results.Range("B3").Value2 Then
                results.Cells(i, "B").Value2 = results.Cells(i, "B").Value2 + .Cells(r, c).Value2
              End If
            Next
            GoTo nextWS 'Delete this line if a letter appears more than once in a column
          End If
        Next
      End With
nextWS: 'Delete this line if a letter appears more than once in a column
    Next
  Next
End Sub

This worked great - thank you!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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