VBA Macro Loop Through worksheet

LearnVBA83

Board Regular
Joined
Dec 1, 2016
Messages
109
Office Version
  1. 365
Platform
  1. Windows
Is there a way to write a macro that will loop through each worksheet in workbook and copy the name of the worksheet into that worksheet in cell A1 to A200?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This is probably cleaner if you don't know the size of the number, try this

VBA Code:
Sub CopySheetNamesWith6DigitNumbersV2()
    Dim ws As Worksheet
    Dim numericPart As String
    Dim i As Integer
    Dim char As String
    
    For Each ws In ThisWorkbook.Worksheets
        numericPart = ""
        
        For i = 1 To Len(ws.Name)
            char = Mid(ws.Name, i, 1)
            If IsNumeric(char) Then
                numericPart = numericPart & char
            End If
        Next
        ws.Range("A1:A200").Value = numericPart
    Next
End Sub
 
Upvote 0
Wow this worked! Is there a way to make the macro format the column as text so that i can get all 6 digits? The reason i'm asking is this is Step 1 to a looping macro i'm trying to build. I also need the macro to add some formulas, do some editing, filtering, deleting, and then to copy and paste that total into a master spreadsheet before it loops to the next worksheet. I assume to do that i'd need to insert the other code that i want to do before the next button. If you're interested in seeing what the total objective i'm trying to accomplish, i created a snagit video below. I understand if you're not interested in looking at it. In that case i will probably post my objectives in separate threads and try to piece it together.

 
Upvote 0
Interesting.

When you are summing expenses, Why are you summing up if the formula is there? You just need the values correct? Do you trust the formulas?

Can you post those two files in a public dropbox or other online file repo?
 
Upvote 0
Interesting.

When you are summing expenses, Why are you summing up if the formula is there? You just need the values correct? Do you trust the formulas?

Can you post those two files in a public dropbox or other online file repo?
Everything in those tabs is hard coded so I need to delete the contingent lines b/c I'm ONLY worried about full time employee wages, benefits, FICA, etc. So I'm removing contingent employee lines and then summing those hard coded numbers so the numbers change to the correct totals. Then I'm copy and pasting values of the whole sheet so that it locks those expense totals. Then filtering by expense and copying those 4 lines to a master template and then loop to the next tab that has a 6 digit name. I had some help and started piecing together some of the sum formula but it still has a glitch or two. What's this drobox? Does it allow me to send you a file?

VBA Code:
Sub test()
lastRow = Cells(Rows.Count, "B").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastRow, 15)) ' load all the dworksheet into memory
startrow = 2
For i = 2 To lastRow
 If inarr(i, 2) = "Expense" Then
  For j = 3 To 15
    tt = Chr(64 + j)
   Range(Cells(i, j), Cells(i, j)).Formula = "=Sum(" & tt & startrow & ":" & tt & i - 1 & ")"
    Next j
    startrow = i + 2
  End If
Next i
End Sub
 
Upvote 0
Don't worry about the code right now. Can you install XL2BB? Ideally there is a sample sheet we can work from. The plugin will allow you to copy one of your "555353" sheets so we can pull it into excel.

Dropbox is a way to share files online and give public access to the forum members. Login - Dropbox
 
Last edited:
Upvote 0
2024 - Budget - Finance Results.xlsx
ABCDEFGHIJKLMNOP
1Done20232023 1RF2023 2RF2023 3RF
2GSO - IREJan FebMarAprMayJunJulAugSepOctNovDec
3Operating Days232121222320232221232122
4
5GSO - IREJan FebMarAprMayJunJulAugSepOctNovDecFull Year
6RPH15.015.015.015.015.015.015.015.015.015.015.015.015.0
7Supervisor2.02.02.02.02.02.02.02.02.02.02.02.02.0
8Tech15.015.015.015.015.015.015.015.015.015.015.015.015.0
22Total FTEs32.032.032.032.032.032.032.032.032.032.032.032.032.0
23QC------------
24
25GSO - IRE - Total CostJan FebMarAprMayJunJulAugSepOctNovDecFull Year
26RPH$ 185,606$ 185,606$ 185,606$ 190,324$ 190,324$ 190,324$ 190,324$ 190,324$ 190,324$ 190,324$ 190,324$ 190,324$ 2,269,730
27Supervisor$ 21,356$ 21,356$ 21,356$ 21,899$ 21,899$ 21,899$ 21,899$ 21,899$ 21,899$ 21,899$ 21,899$ 21,899$ 261,162
28Tech$ 70,905$ 64,740$ 64,740$ 69,442$ 72,598$ 63,129$ 72,598$ 69,442$ 66,285$ 72,598$ 66,285$ 69,442$ 822,202
42Expense277,868271,702271,702281,664284,821275,351284,821281,664278,508284,821278,508281,664$ 3,353,094
43
44GSO - IRE - WagesJan FebMarAprMayJunJulAugSepOctNovDecFull Year
45RPH157,253157,253157,253157,253157,253157,253157,253157,253157,253157,253157,253157,253$ 1,887,039
46Supervisor18,09418,09418,09418,09418,09418,09418,09418,09418,09418,09418,09418,094$ 217,128
47Tech56,42651,52051,52053,97356,42649,06656,42653,97351,52056,42651,52053,973$ 642,768
61Expense231,773226,867226,867229,320231,773224,414231,773229,320226,867231,773226,867229,3202,746,935
62
63GSO - IRE - MeritJan FebMarAprMayJunJulAugSepOctNovDecFull Year
64RPH4,7184,7184,7184,7184,7184,7184,7184,7184,718$ 42,458
65Supervisor543543543543543543543543543$ 4,885
66Tech1,6191,6931,4721,6931,6191,5461,6931,5461,619$ 14,499
80Expense---6,8806,9536,7326,9536,8806,8066,9536,8066,88061,843
81
82GSO - IRE - FICAJan FebMarAprMayJunJulAugSepOctNovDecFull Year
83RPH13,067.713,067.713,067.713,067.713,067.713,067.713,067.713,067.713,067.713,067.713,067.713,067.7$ 156,813
84Supervisor1,503.61,503.61,503.61,503.61,503.61,503.61,503.61,503.61,503.61,503.61,503.61,503.6$ 18,043
85Tech4,689.04,281.34,281.34,485.14,689.04,077.44,689.04,485.14,281.34,689.04,281.34,485.1$ 53,414
99Expense19,26018,85318,85319,05719,26018,64919,26019,05718,85319,26018,85319,057228,270
100
101GSO - IRE - BenefitsJan FebMarAprMayJunJulAugSepOctNovDecFull Year
102RPH$ 15,285$ 15,285$ 15,285$ 15,285$ 15,285$ 15,285$ 15,285$ 15,285$ 15,285$ 15,285$ 15,285$ 15,285$ 183,420
103Supervisor$ 1,759$ 1,759$ 1,759$ 1,759$ 1,759$ 1,759$ 1,759$ 1,759$ 1,759$ 1,759$ 1,759$ 1,759$ 21,105
104Tech$ 9,790$ 8,939$ 8,939$ 9,364$ 9,790$ 8,513$ 9,790$ 9,364$ 8,939$ 9,790$ 8,939$ 9,364$ 111,520
118Expense26,83425,98225,98226,40826,83425,55726,83426,40825,98226,83425,98226,408316,045
008179
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C23:O23Cell Value<>0textNO
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,838
Members
449,343
Latest member
DEWS2031

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