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
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,135
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]
 

Salts

New Member
Joined
Aug 21, 2019
Messages
16
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.
 

Salts

New Member
Joined
Aug 21, 2019
Messages
16
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]
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,135
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:

Salts

New Member
Joined
Aug 21, 2019
Messages
16
Makes sense, simple enough & works perfectly. AlphaFrog - you are amazing & very helpful. Thank you!
 

Forum statistics

Threads
1,077,614
Messages
5,335,253
Members
399,009
Latest member
twcaddell

Some videos you may like

This Week's Hot Topics

Top