Run VBA on all open tabs

Dannottheman

Board Regular
Joined
Dec 24, 2020
Messages
55
Office Version
  1. 2007
Hi,

I searched but could not find an answer to this. How can I make the 3 VBAs below run on all the open sheets/tabs in my Excel? Right now I have to manually click on each each sheet and "execute", "execute", "execute", etc...
Ideally, I would have 20 open spreadsheets and the VBAs would apply to all of them. Right now I have to click at least 100 to 150 times to make the changes in 20 sheets when in reality it would be great to click just 5 to 10 times total. Thank you in advance as this task is stressing me out and I have hundreds of these sheets.

VBA 1:
class=prism-token>Option Explicit
Sub Dannottheman3()
Dim Cl As Range
Dim srch As Variant
For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
For Each srch In Array("quiz", "unit", "exam", class=prism-token>"examen", "assessment", "test") ' <= note the different use of the jolly (could be useful)
If " " & LCase(Cl.Value) & " " Like "*[!a-z0-9]" & srch & "[!a-z0-9]*" Then
Cl.Interior.Color = rgbLightBlue
Cl.Font.Bold = True
Exit For
End If
Next srch
Next Cl
End Sub

VBA 2:
Sub Test3()
Application.ScreenUpdating = False
With ActiveSheet.Columns("A")
.ColumnWidth = 95
.WrapText = True
End With

VBA 3:
Columns("A:B").NumberFormat = "General"
Application.ScreenUpdating = True
End Sub


VBA 4:
Insert Row
Sub testit2()
Worksheets(1).Cells(1, 1).EntireRow.Insert
Debug.Print "| &"; Worksheets(1).Name; "& |", Len(Worksheets(1).Name)
End Sub


VBA 5:
Clear format:
Sub clearformat()
With ActiveSheet.Columns("A")
.Columns(1).ClearFormats 'clear formatting from column A
End With
End Substyle='font-size:9.0pt;font-family:Consolas'>
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
try this:
VBA Code:
Sub test()
Dim wb As Workbook
Dim ws As Worksheet
For Each wb In Application.Workbooks
 If wb.Name <> "PERSONAL.XLSB" Then
   For Each ws In wb.Sheets
   Call vba1
   'etc
   Next ws
 End If
Next wb
End Sub
 
Upvote 0
try this:
VBA Code:
Sub test()
Dim wb As Workbook
Dim ws As Worksheet
For Each wb In Application.Workbooks
If wb.Name <> "PERSONAL.XLSB" Then
   For Each ws In wb.Sheets
   Call vba1
   'etc
   Next ws
End If
Next wb
End Sub
It give an error "Compilation error" Sub or Function not defined
 
Upvote 0
You need to change
VBA Code:
Call VBA1
to what ever the name is of the sub routines you want to run e.g test3 ()
 
Upvote 0
I did but it doesn't work.
You need to change
VBA Code:
Call VBA1
to what ever the name is of the sub routines you want to run e.g test3 ()
I did. Still shows the same error...
 

Attachments

  • error.png
    error.png
    24.1 KB · Views: 20
Upvote 0
try running this which should show you that the loop is correctly selecting each of the worksheets in each of the workbooks. If you are getting the sub undefined error it is something to do with where you are putting the subrouitned:
VBA Code:
Sub test()
Dim wb As Workbook
Dim ws As Worksheet
For Each wb In Application.Workbooks
 If wb.Name <> "PERSONAL.XLSB" Then
   For Each ws In wb.Sheets
 MsgBox (wb.Name & " / " & ws.Name)
     'etc
   Next ws
 End If
Next wb
End Sub
 
Upvote 0
There's no dannotheman3 Sub in this module, so there's the error.
Whre's this sub?
 
Upvote 0
try running this which should show you that the loop is correctly selecting each of the worksheets in each of the workbooks. If you are getting the sub undefined error it is something to do with where you are putting the subrouitned:
VBA Code:
Sub test()
Dim wb As Workbook
Dim ws As Worksheet
For Each wb In Application.Workbooks
If wb.Name <> "PERSONAL.XLSB" Then
   For Each ws In wb.Sheets
MsgBox (wb.Name & " / " & ws.Name)
     'etc
   Next ws
End If
Next wb
End Sub
Please see attached image
 

Attachments

  • error2.png
    error2.png
    24.7 KB · Views: 16
Upvote 0
There's no dannotheman3 Sub in this module, so there's the error.
Whre's this sub?
I tried placing the dannottheman2 Sub in different places of the module and it would still show the same error. For example:

Sub test()
Dim wb As Workbook
Dim ws As Worksheet
For Each wb In Application.Workbooks
If wb.Name <> "PERSONAL.XLSB" Then
For Each ws In wb.Sheets
Call Dannottheman3
Next ws
End If
Next wb
End Sub

Option Explicit
Sub Dannottheman3()
Dim Cl As Range
Dim srch As Variant
For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
For Each srch In Array("Academy") ' <= note the different use of the jolly (could be useful)
If " " & LCase(Cl.Value) & " " Like "*[!a-z0-9]" & srch & "[!a-z0-9]*" Then
Cl.Interior.Color = rgbLightBlue
Cl.Font.Bold = True
Exit For
End If
Next srch
Next Cl
End Sub
 
Upvote 0
Which line is the error on, I suspect array Academy is not defined
 
Upvote 0

Forum statistics

Threads
1,214,394
Messages
6,119,262
Members
448,880
Latest member
aveternik

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