VBA sum up the multiple spreadsheet with criteria based on their sheet name

Cloris

New Member
Joined
Oct 24, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi could someone know how to code this macro? Is it correct? or does it has easier way to code this? like SUMIFS?

If there are 30 products, so there are 30 worksheet, and this product code will further split into 3 different categories. So I will have to create additional 3 tabs for each categories and need to sum up the values inside this 30 worksheets for respective categories.

Product start with C and end with 6 is called Lion
Product start with C and end with 3 is called Tiger
Product start with U and end with 4 is called Bird

VBA Code:
Sub Macro45()
'
' Macro45 Macro"CrossMult_Validation")

'
Dim ws As Worksheet
Dim lLastRow As Long, i As Long
Dim RunningSum As Range

For Each ws In ThisWorkbook.Worksheets
    If Left(ws.Name, 1) = "C" And Right(ws.Name, 1) = "6" Then
        With ws
        lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Parent.Sheets.Add After:=.Parent.Sheets(.Parent.Sheets.Count)
       
        For i = 1 To lastRowData
            .Cells(14, i + 1) = Application.WorksheetFunction.Sum(RunningSum)
        Next i
        .Name = "Lion"
        End With

For Each ws In ThisWorkbook.Worksheets
    If Left(ws.Name, 1) = "C" And Right(ws.Name, 1) = "3" Then
        With ws
        lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Parent.Sheets.Add After:=.Parent.Sheets(.Parent.Sheets.Count)

        For i = 1 To lastRowData
            .Cells(14, i + 1) = Application.WorksheetFunction.Sum(RunningSum)
        Next i
        .Name = "Tiger"
        End With


For Each ws In ThisWorkbook.Worksheets
    If Left(ws.Name, 1) = "U" And Right(ws.Name, 1) = "4" Then
        With ws
        lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Parent.Sheets.Add After:=.Parent.Sheets(.Parent.Sheets.Count)

        For i = 1 To lastRowData
            .Cells(14, i + 1) = Application.WorksheetFunction.Sum(RunningSum)
        Next i
        .Name = "Bird"
        End With
     End If
    Next ws

End Sub

Thank you ! :)
 

Attachments

  • yoo.JPG
    yoo.JPG
    43.7 KB · Views: 11

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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