Repeating a Macro across an Entire Workbook

Nicole_Michelle01

Board Regular
Joined
Nov 9, 2005
Messages
50
I have 5 worksheets (Sheet1, Sheet2, Sheet3, Sheet4, Sheet5) and I want the following macro run for each worksheet:

Sub SetupFile()
For intcount = 1 To 256
If InStr(1, Cells(1, intcount), "userpsswd") > 0 Then
Cells(1, intcount).EntireColumn.Delete
End If
Next intcount
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Range("A1").Select
ActiveCell.FormulaR1C1 = "Review Notes"
Columns("A:A").Select
Selection.Columns.AutoFit
Columns("E:E").Select
Selection.Insert Shift:=xlToRight
Range("E1").Select
ActiveCell.FormulaR1C1 = "Dept."
End Sub

And also, if I want to add additional keywords to the InStr row ie "userpsswd" "psswd_expdate" "psswd_createdate", what would that look like?

thanks!
 
Okay, try this and let me know how it works out.
Code:
Option Explicit
Sub SetupAllFiles()
Dim ws As Worksheet
Dim SearchWord, i As Integer

Application.ScreenUpdating = False
SearchWord = Array("keyword1", "keyword2", "keyword3", "keyword4")
For i = 0 To UBound(SearchWord)
    For Each ws In ActiveWorkbook.Worksheets
        Call SetupFile(SearchWord(i), ws)
    Next ws
Next i
Application.ScreenUpdating = True
End Sub


Sub SetupFile(KeywordPassed, ActiveWs As Worksheet)
Dim intcount As Integer

For intcount = 1 To 256
    If InStr(1, ActiveWs.Cells(1, intcount), KeywordPassed) > 0 Then
        ActiveWs.Cells(1, intcount).EntireColumn.Delete
    End If
Next intcount
ActiveWs.Columns("A:A").Insert Shift:=xlToRight
ActiveWs.Range("A1").FormulaR1C1 = "Review Notes"
ActiveWs.Columns("A:A").Columns.AutoFit
ActiveWs.Columns("E:E").Insert Shift:=xlToRight
ActiveWs.Range("E1").FormulaR1C1 = "Dept."
End Sub

Regards,
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Now I added the keywords and it's deleting all the data on every worksheet except one:

Option Explicit
Sub SetupAllFiles()
Dim ws As Worksheet
Dim SearchWord, i As Integer

Application.ScreenUpdating = False
SearchWord = Array("emp_title", "hiredate", "lastincdate", "lastincpct", "psswd_expdate", "staffaddress1", "staffaddress2", "staffcity", "staffcomment", "staffgrade", "staffphone", "staffsalary", "staffssno", "staffstate", "staffzip", "userpsswd")
For i = 0 To UBound(SearchWord)
For Each ws In ActiveWorkbook.Worksheets
Call SetupFile(SearchWord(i), ws)
Next ws
Next i
Application.ScreenUpdating = True
End Sub

Sub SetupFile(KeywordPassed, ActiveWs As Worksheet)
Dim intcount As Integer

For intcount = 1 To 256
If InStr(1, ActiveWs.Cells(1, intcount), KeywordPassed) > 0 Then
ActiveWs.Cells(1, intcount).EntireColumn.Delete
End If
Next intcount
ActiveWs.Columns("A:A").Insert Shift:=xlToRight
ActiveWs.Range("A1").FormulaR1C1 = "Review Notes"
ActiveWs.Columns("A:A").Columns.AutoFit
ActiveWs.Columns("E:E").Insert Shift:=xlToRight
ActiveWs.Range("E1").FormulaR1C1 = "Dept."
End Sub

what did I do wrong?
 
Upvote 0
YIKES :eek: That's the hazard of trying to solve a problem without real data.

How about this? :biggrin:
Code:
Option Explicit
Sub SetupAllFiles()
Dim ws As Worksheet
Dim SearchWord, i As Integer

Application.ScreenUpdating = False
SearchWord = Array("keyword1", "keyword2", "keyword3", "keyword4")
For Each ws In ActiveWorkbook.Worksheets
    For i = 0 To UBound(SearchWord)
        Call SetupFile(SearchWord(i), ws)
    Next i
    ws.Columns("A:A").Insert Shift:=xlToRight
    ws.Range("A1").FormulaR1C1 = "Review Notes"
    ws.Columns("A:A").Columns.AutoFit
    ws.Columns("E:E").Insert Shift:=xlToRight
    ws.Range("E1").FormulaR1C1 = "Dept."
Next ws
Application.ScreenUpdating = True
End Sub


Sub SetupFile(KeywordPassed, ActiveWs As Worksheet)
Dim intcount As Integer

For intcount = 1 To 256
    If InStr(1, ActiveWs.Cells(1, intcount), KeywordPassed) > 0 Then
        ActiveWs.Cells(1, intcount).EntireColumn.Delete
    End If
Next intcount
End Sub
 
Upvote 0
it works except for one of them, emp_title.

There are three headers with "emp_title" in the header

COLY: emp_title_collec, COLZ: emp_title_limit, COLAA: emp_title

of the three, it is deleting emp_title_collec (which is incorrect) and emp_title (which is correct)
 
Upvote 0
Nicole_Michelle01 said:
it works except for one of them, emp_title.

There are three headers with "emp_title" in the header

COLY: emp_title_collec, COLZ: emp_title_limit, COLAA: emp_title

of the three, it is deleting emp_title_collec (which is incorrect) and emp_title (which is correct)

Okay, so you're deleting exact matches only (by the way, your first code doesn't do that)?
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,925
Members
449,094
Latest member
teemeren

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