Running the Same Macro on Multiple Worksheets and Multiple Files

stroffso

Board Regular
Joined
Jul 12, 2016
Messages
160
Office Version
  1. 365
Platform
  1. Windows
I have a piece of code called "restoredataval" which i need to apply to certain tabs in about 15 workbooks all of which have a varying number of tabs.

What i want to do is open the file, select the tabs I need the macro to run on and then press run. The macro should then Run through each tab selected one by one. For context the macro is restoring data validation settings to each of the tabs selected. Also the tab names on each workbook are vastly different so I cant specify arrays to run it on however a list of all the tab names I need are on a tab called Controls if that helps

Thanks in advance
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
1) Create a new workbook. This will be macro workbook where you run your macro from.
2) Create a userform with a CommandButton (I use default name here UserForm1) to pause execution to you time to select tabs/sheets

The code for UserForm1 under CommandButton (I put caption Continue)click:
VBA Code:
Private Sub CommandButton1_Click()
Unload Me
End Sub

The code under normal module:
VBA Code:
Sub LoopSelectedSheets()

Dim wb As Workbook
Dim ws As Worksheet
Dim wsSelect As Object

' Select workbook
Fname = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls; *.xlsx), *.xls; *.xlsx", Title:="Select a File")
If Fname = False Then Exit Sub                         'CANCEL is clicked
' Define workbook as wb when opening it
Set wb = Workbooks.Open(Filename:=Fname, UpdateLinks:=False)

UserForm1.Show vbModeless
Do While UserForm1.Visible = True
    DoEvents
Loop

Set wsSelect = ActiveWindow.SelectedSheets

For Each ws In wsSelect

    ' Call your macro here

Next ws

End Sub

When the code is run, you will be prompt to select a workbook. Once workbook opened, button Continue will pop-up which can easily moved (vbModeless). Thenselect all your tabs of interest and click continue to execute your remaining code.

Opss. Look like I have no use of wb parameter but leave it there in case you need to call wb name ?
 
Upvote 0
Solution
Thats great thank you, for some reason it keeps erroring at the below row but only after I have selected multiple tabs and continued on the userform. If I run this one tab at a time it works fine

VBA Code:
   Range("D6:D7").Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="='Project Drops'!$BB$2:$BB$23"
 
Upvote 0
It is important to specify the worksheet you are working on, else macro can get confused.
Instead of
Range("D6:D7").Select
try
ws.Range("D6:D7").Select

this will specify range on which sheet, not any other sheets. I think this will solve the problem
 
Upvote 0
Of course, thank you so much, still errored but I think i know what to do from here thanks
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,834
Messages
6,127,164
Members
449,368
Latest member
JayHo

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