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?
 
2024 - Budget - Finance Results.xlsx
ABCDEFGHIJKLMNOP
1Done20242024 1RF2024 2RF2024 3RF
2CSO - Commercial PA ProdJan FebMarAprMayJunJulAugSepOctNovDec
3Operating Days232121222320232221232122
4
5CSO - Commercial PA ProdJan FebMarAprMayJunJulAugSepOctNovDecFull Year
6Coordinator15.015.015.015.015.015.015.015.015.015.015.015.015.0
7Manager5.05.05.05.05.05.05.05.05.05.05.05.05.0
8RPH174.3180.9203.3172.2167.9188.1135.7169.0156.6152.2167.8160.0169.0
9RPH - Contingent70.275.952.759.090.883.791.171.571.881.995.8101.978.9
10Sr Manager1.01.01.01.01.01.01.01.01.01.01.01.01.0
11Supervisor44.843.144.338.041.244.738.040.538.440.547.647.842.4
12Tech428.9425.3431.9351.6372.1401.0328.7343.8322.9362.3468.6496.1394.4
13Tech - Contingent107.168.683.175.484.8105.4103.1118.7114.5106.299.076.395.2
14Sr Analyst-------------
15Consultant-------------
16Nurse Associate10.010.010.010.010.010.010.010.010.010.010.010.010.0
170-------------
180-------------
190-------------
200-------------
210-------------
22Total FTEs856.2824.8846.2727.2787.7853.9727.6774.5735.2774.1909.7913.0810.8
23QC-----
24
25CSO - Commercial PA Prod - Total CostJan FebMarAprMayJunJulAugSepOctNovDecFull Year
26Coordinator$ 64,923$ 59,277$ 59,277$ 63,583$ 66,473$ 57,803$ 66,473$ 63,583$ 60,693$ 66,473$ 60,693$ 63,583$ 752,833
27Manager$ 62,739$ 62,739$ 62,739$ 64,334$ 64,334$ 64,334$ 64,334$ 64,334$ 64,334$ 64,334$ 64,334$ 64,334$ 767,224
28RPH$ 2,146,169$ 2,227,050$ 2,502,611$ 2,174,530$ 2,119,411$ 2,374,398$ 1,712,733$ 2,133,894$ 1,977,175$ 1,921,766$ 2,118,222$ 2,019,767$ 25,427,726
29RPH - Contingent$ 1,031,463$ 1,018,044$ 707,026$ 829,196$ 1,334,297$ 1,070,120$ 1,338,256$ 1,004,925$ 963,616$ 1,203,879$ 1,285,267$ 1,432,185$ 13,218,273
30Sr Manager$ 10,409$ 10,409$ 10,409$ 10,674$ 10,674$ 10,674$ 10,674$ 10,674$ 10,674$ 10,674$ 10,674$ 10,674$ 127,294
31Supervisor$ 324,394$ 312,415$ 320,570$ 282,184$ 305,847$ 331,727$ 282,318$ 300,671$ 285,291$ 300,516$ 353,584$ 354,848$ 3,754,367
32Tech$ 1,974,704$ 1,788,143$ 1,815,684$ 1,585,551$ 1,754,189$ 1,644,085$ 1,549,801$ 1,550,469$ 1,389,852$ 1,707,948$ 2,016,974$ 2,237,054$ 21,014,454
33Tech - Contingent$ 489,534$ 286,394$ 346,902$ 329,883$ 387,732$ 418,926$ 471,517$ 519,079$ 477,907$ 485,722$ 413,316$ 333,528$ 4,960,439
34Sr Analyst$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
35Consultant$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
36Nurse Associate$ 66,638$ 60,844$ 60,844$ 65,263$ 68,229$ 59,330$ 68,229$ 65,263$ 62,296$ 68,229$ 62,296$ 65,263$ 772,724
370$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
380$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
390$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
400$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
410$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
42Expense6,170,9755,825,3165,886,0645,405,1976,111,1856,031,3965,564,3345,712,8905,291,8395,829,5406,385,3606,581,236$ 70,795,334
001128
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C23:O23Cell Value<>0textNO
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
2024 - Budget - Finance Results.xlsx
ABCDEFGHIJKLMNOP
43
44CSO - Commercial PA Prod - WagesJan FebMarAprMayJunJulAugSepOctNovDecFull Year
45Coordinator51,66647,17347,17349,41951,66644,92751,66649,41947,17351,66647,17349,419$ 588,538
46Manager53,15553,15553,15553,15553,15553,15553,15553,15553,15553,15553,15553,155$ 637,865
47RPH1,818,3251,886,8502,120,3181,796,6871,751,1451,961,8261,415,1311,763,1121,633,6241,587,8431,750,1631,668,815$ 21,153,839
48RPH - Contingent1,031,4631,018,044707,026829,1961,334,2971,070,1201,338,2561,004,925963,6161,203,8791,285,2671,432,185$ 13,218,273
49Sr Manager8,8198,8198,8198,8198,8198,8198,8198,8198,8198,8198,8198,819$ 105,831
50Supervisor274,840264,692271,601233,152252,703274,087233,262248,427235,719248,299292,146293,190$ 3,122,119
51Tech1,571,4661,423,0011,444,9181,232,3571,363,4301,277,8521,204,5711,205,0901,080,2521,327,4891,567,6781,738,733$ 16,436,838
52Tech - Contingent489,534286,394346,902329,883387,732418,926471,517519,079477,907485,722413,316333,528$ 4,960,439
53Sr Analyst------------$ -
54Consultant------------$ -
55Nurse Associate53,03148,41948,41950,72553,03146,11453,03150,72548,41953,03148,41950,725$ 604,088
560------------$ -
570------------$ -
580------------$ -
590------------$ -
600------------$ -
61Expense5,352,3005,036,5485,048,3324,583,3945,255,9785,155,8264,829,4084,902,7514,548,6865,019,9025,466,1365,628,57160,827,831
62
63CSO - Commercial PA Prod - MeritJan FebMarAprMayJunJulAugSepOctNovDecFull Year
64Coordinator1,4831,5501,3481,5501,4831,4151,5501,4151,483$ 13,276
65Manager1,5951,5951,5951,5951,5951,5951,5951,5951,595$ 14,352
66RPH53,90152,53458,85542,45452,89349,00947,63552,50550,064$ 459,850
67RPH - Contingent---------$ -
68Sr Manager265265265265265265265265265$ 2,381
69Supervisor6,9957,5818,2236,9987,4537,0727,4498,7648,796$ 69,330
70Tech36,97140,90338,33636,13736,15332,40839,82547,03052,162$ 359,924
71Tech - Contingent---------$ -
72Sr Analyst---------$ -
73Consultant---------$ -
74Nurse Associate1,5221,5911,3831,5911,5221,4531,5911,4531,522$ 13,627
750---------$ -
760---------$ -
770---------$ -
780---------$ -
790---------$ -
80Expense---102,729106,018110,00390,589101,36293,21599,909113,027115,886932,739
001128
 
Upvote 0
2024 - Budget - Finance Results.xlsx
ABCDEFGHIJKLMNOP
81
82CSO - Commercial PA Prod - FICAJan FebMarAprMayJunJulAugSepOctNovDecFull Year
83Coordinator4,293.43,920.13,920.14,106.74,293.43,733.44,293.44,106.73,920.14,293.43,920.14,106.7$ 48,908
84Manager4,417.24,417.24,417.24,417.24,417.24,417.24,417.24,417.24,417.24,417.24,417.24,417.2$ 53,007
85RPH151,102.8156,797.3176,198.4149,304.7145,520.2163,027.7117,597.4146,514.6135,754.2131,949.7145,438.5138,678.5$ 1,757,884
86RPH - Contingent
87Sr Manager732.9732.9732.9732.9732.9732.9732.9732.9732.9732.9732.9732.9$ 8,795
88Supervisor22,839.221,995.922,570.019,375.020,999.722,776.619,384.120,644.319,588.320,633.724,277.324,364.1$ 259,448
89Tech130,588.8118,251.4120,072.7102,408.9113,301.0106,189.5100,099.8100,143.089,768.9110,314.4130,274.0144,488.7$ 1,365,901
90Tech - Contingent
91Sr Analyst------------$ -
92Consultant------------$ -
93Nurse Associate4,406.84,023.64,023.64,215.24,406.83,832.04,406.84,215.24,023.64,406.84,023.64,215.2$ 50,200
940------------$ -
950------------$ -
960------------$ -
970------------$ -
980------------$ -
99Expense318,381310,138331,935284,561293,671304,709250,932280,774258,205276,748313,084321,0033,544,142
100
101CSO - Commercial PA Prod - BenefitsJan FebMarAprMayJunJulAugSepOctNovDecFull Year
102Coordinator$ 8,964$ 8,185$ 8,185$ 8,574$ 8,964$ 7,795$ 8,964$ 8,574$ 8,185$ 8,964$ 8,185$ 8,574$ 102,111
103Manager$ 5,167$ 5,167$ 5,167$ 5,167$ 5,167$ 5,167$ 5,167$ 5,167$ 5,167$ 5,167$ 5,167$ 5,167$ 62,000
104RPH$ 176,741$ 183,402$ 206,095$ 174,638$ 170,211$ 190,689$ 137,551$ 171,374$ 158,788$ 154,338$ 170,116$ 162,209$ 2,056,153
105RPH - Contingent
106Sr Manager$ 857$ 857$ 857$ 857$ 857$ 857$ 857$ 857$ 857$ 857$ 857$ 857$ 10,287
107Supervisor$ 26,714$ 25,728$ 26,400$ 22,662$ 24,563$ 26,641$ 22,673$ 24,147$ 22,912$ 24,135$ 28,397$ 28,498$ 303,470
108Tech$ 272,649$ 246,891$ 250,693$ 213,814$ 236,555$ 221,707$ 208,993$ 209,083$ 187,424$ 230,319$ 271,992$ 301,670$ 2,851,791
109Tech - Contingent
110Sr Analyst$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
111Consultant$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
112Nurse Associate$ 9,201$ 8,401$ 8,401$ 8,801$ 9,201$ 8,001$ 9,201$ 8,801$ 8,401$ 9,201$ 8,401$ 8,801$ 104,809
1130$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
1140$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
1150$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
1160$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
1170$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -$ -
118Expense500,294478,630505,797434,513455,518460,858393,406428,004391,733432,981493,114515,7765,490,622
119
120
001128
 
Upvote 0
I put the worksheet in... in 3 sections. The tab name is 001128

Please let me know if that works! Thanks again! I'm learning a lot looking through your code. It seemed like when i was walking through the code F8 that it was deleting all the lines from this worksheet... so it may be trying to delete everything that's not contingent instead of deleting the lines that are contingent. Also, most of these worksheets have active filters so you may want the code to remove filter at the beginning.
 
Upvote 0
hmmm

It worked for me

The filters are being turned off prior any modifications already.

Change this part of the sub
VBA Code:
                ' delete contingent
                For i = ws.UsedRange.Rows.Count To 1 Step -1
                    If InStr(1, ws.Range("C" & i).Value, "Contingent", vbTextCompare) > 0 Then
                       ws.Cells(i, 1).EntireRow.Delete  ' more descriptive
                    End If
                Next

here are the results

Master.xlsm
ABCDEFGHIJKLMN
2001128617097558253165886064540519761111856031396556433457128905291839582954063853606581236
30011286120110535230050365485048332458339452559785155826482940849027514548686501990254661365628571
40011286120807000102729.5106018.5110003.490589.05101362.493214.8899909.06113026.6115885.7
50011286120605318381.2310138.4331935284560.6293671.2304709.4250931.7280773.9258205.2276748.1313083.7321003.4
60011286030610500293.8478629.8505797434513.3455517.9460857.5393405.6428003.6391733.1432981.1493113.7515776
Master
 
Upvote 0
That's crazy it works for you and not me. I put in the new code and still not working... could it be the version? I have Microsoft® Excel® for Microsoft 365 MSO (Version 2208 Build 16.0.15601.20796) 32-bit I wonder if the code is trying to run too fast.

I made a 1 min snagit. Let me know if you need me to redo. The Audio isn't the best.

 
Upvote 0
I doubt it's the version. Nothing crazy in the code.

I didn't see any reason from the video that it shouldn't have worked. I just ran it again and it worked.

See if you can get that file put up on dropbox and I'll look at it.

 
Upvote 0
Does this need to be reversed? If you look at your totals you posted above they don't tie to what the totals should be without the contingent. I think it's b/c you do the paste value before deleting the contingent. I was able to get it to work once by holding down F8 for a long time!

VBA Code:
 ' overwrite with just values
                With .UsedRange
                    .Value = .Value
                End With
            
                ' delete contingent
                For i = ws.UsedRange.Rows.Count To 1 Step -1
                    If InStr(1, ws.Range("C" & i).Value, "Contingent", vbTextCompare) > 0 Then
                       ws.Cells(i, 1).EntireRow.Delete  ' more descriptive
                    End If
                Next
 
Upvote 0
I logged into dropbox and uploaded the file and the master template. Now how to I get it to you?
 
Upvote 0

Forum statistics

Threads
1,216,368
Messages
6,130,201
Members
449,566
Latest member
MikeWnbExclWiz

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