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

Salts

New Member
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
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
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
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
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
Makes sense, simple enough & works perfectly. AlphaFrog - you are amazing & very helpful. Thank you!
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top