Macro to loop thru sheets adding new columns until it hits particular sheet then stops

Salts

New Member
Joined
Aug 21, 2019
Messages
16
Trying to write a macro that will add 2 new columns (A & B) to each sheet in the workbook, adding header names & color coding, but need it to stop when it reaches the tab named "Master"
Below is the code I have so far, but keep getting a compile error in the 'Start loop' section

Sub Insert_Columns()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

On Error Resume Next

'adds new column A & B into each worksheet
ws.[a:b].Insert

'places header in new columns
ws.Range("A1").Value = "Date Deleted: After Master Tab is Created"
ws.Range("B1").Value = "Date Added: After Master Tab is Created"

'Color codes the header "RED" in column A
With ws.Range("A:A").Font
.Color = -16776961
.TintAndShade = 0
.Bold = True
End With

'Color codes the header "Green" in column B
With ws.Range("B:B").Font
.Color = -11489280
.TintAndShade = 0
.Bold = True
End With

With ws.Rows("1:1").EntireRow.AutoFit
End With

'Start loop
For Each sht In ws.Worksheets
'If worksheet in loop is the last one, stop execution (it is Master worksheet)
If sht.Index = ws.Worksheets Then
Exit For
End If

'when loop reaches "Master tab then it stops _
copying tabs to master tab
If sht.Name = "Master" Then
Exit For
End If

Next ws

End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
[color=darkblue]Sub[/color] Insert_Columns()
    
    [color=darkblue]Dim[/color] ws [color=darkblue]As[/color] Worksheet
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] ws [color=darkblue]In[/color] ActiveWorkbook.Worksheets
        
        [color=darkblue]If[/color] ws.Name = "Master" [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]For[/color]
        
        [color=green]'adds new column A & B into each worksheet[/color]
        ws.[a:b].Insert
        
        [color=green]'places header in new columns[/color]
        ws.Range("A1").Value = "Date Deleted: After Master Tab is Created"
        ws.Range("B1").Value = "Date Added: After Master Tab is Created"
        
        [color=green]'Color codes the header "RED" in column A[/color]
        [color=darkblue]With[/color] ws.Range("A:A").Font
            .Color = -16776961
            .TintAndShade = 0
            .Bold = [color=darkblue]True[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        
        [color=green]'Color codes the header "Green" in column B[/color]
        [color=darkblue]With[/color] ws.Range("B:B").Font
            .Color = -11489280
            .TintAndShade = 0
            .Bold = [color=darkblue]True[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        
        ws.Rows("1:1").EntireRow.AutoFit
        
    [color=darkblue]Next[/color] ws
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
So you just put the
Code:
 Name= "Master" Then Exit For [code] at the beginning and don't need the loop thru section.
Sweet.  Appreciate the help.
 
Upvote 0
So you just put the
Code:
 Name= "Master" Then Exit For [code] at the beginning and don't need the loop thru section.
Sweet.  Appreciate the help.[/QUOTE]


Want to use this same premise to go back and hide certain column in all worksheets except for "Master" sheet - except when I run the following code - it only works in the "Master" tab, 
it does not hide these columns in any of the other worksheets.

[code]For Each ws In ActiveWorkbook.Worksheets
                
                If ws.Name = "Master" Then Exit For
                
    Range("A:B,D:D,L:L,N:N,Q:S,V:V,Y:Y,AB:AB,AE:AE,AK:AL").Select
    Selection.EntireColumn.Hidden = True
   
    Range("C1").Select
                
            Next ws
    End Sub [end code]
 
Upvote 0
Code:
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] ws [COLOR=darkblue]In[/COLOR] ActiveWorkbook.Worksheets
    
        [COLOR=darkblue]If[/COLOR] ws.Name = "Master" [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]For[/COLOR]
    
        [COLOR=#ff0000][B]ws.[/B][/COLOR]Range("A:B,D:D,L:L,N:N,Q:S,V:V,Y:Y,AB:AB,AE:AE,AK:AL").EntireColumn.Hidden = [COLOR=darkblue]True[/COLOR]
    
    [COLOR=darkblue]Next[/COLOR] ws
 
Last edited:
Upvote 0
Makes sense, simple enough & works perfectly. AlphaFrog - you are amazing & very helpful. Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,704
Messages
6,173,984
Members
452,540
Latest member
haasro02

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