Loop Any Macro in Multiple Sheets in Same Workbook

Status
Not open for further replies.
Joined
Jan 11, 2024
Messages
19
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi Everyone, I am using a macro in a single sheet of a workbook. If I want to use same macro in the other sheet of the same workbook then I have to use same macro again. Is there any loop method by which a macro can be run automatically in multiple sheets of the same workbook?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Welcome to the Board!

This link shows you how to loop through different sheets in your workbook: Macro to Loop Through All Worksheets in a Workbook - Microsoft Support
Just be sure to put this code in a General module in VBA, and not in any particular sheet module.
And note that you will either need to qualify all of your range references with the sheet reference, or select/activate the sheet within the loop.

If you run into any issues, post your VBA code, as described here: How to Post Your VBA Code
 
Upvote 0
Sub ColorMispelledCells()
Dim xRg As Range, xCell As Range
On Error Resume Next
Set xRg = Application.InputBox("please select range:", "KuTools For Excel", Selection.Address, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
For Each xCell In xRg
If Not Application.CheckSpelling(xCell.Text) Then _
xCell.Interior.ColorIndex = 28
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Didn't want to use the code tags for readability, eh?

That VBA code you posted prompts the user to select a range.
Do you want it to select a range for each and every sheet?
If not, how to you want it to work?
 
Upvote 0
Didn't want to use the code tags for readability, eh?

That VBA code you posted prompts the user to select a range.
Do you want it to select a range for each and every sheet?
If not, how to you want it to work?
No I dont want to select any range.
 
Upvote 0
No I dont want to select any range.
That is an integral part of the code you posted.
If you do not want to select any range, then you need to tell us how you want it to work.
Is there some specific range on every sheet that you want it to run against?
We need to know the requirements for what we are building, since you want to change how it works.
 
Upvote 0
See in this Wrokbook there are 5 separate sheets names as 2001 2002 2003 2004 and 2005. All these sheets have words in particular columns separated by space delimiter, I want to check error in all the sheets. I used above macro for 1 sheets and spelling mistakes are highlighted in blue color. But i have to run that macro in all sheets separately. So i want to loop the macro for all sheets at once.
 

Attachments

  • Error checking sheet.jpg
    Error checking sheet.jpg
    55 KB · Views: 3
Upvote 0
OK, I am guessing that you just want to to run against ALL the data on each sheet, regardless of where it ends.
If that is the case, try this:
VBA Code:
Sub ColorMispelledCells()

    Dim xRg As Range, xCell As Range
    Dim ws As Worksheet

    On Error Resume Next
    
'   Loop through all sheets
    For Each ws In Worksheets
    
'       Select sheet
        ws.Activate
'       Capture used range on sheet
        Set xRg = ws.UsedRange
    
        If xRg Is Nothing Then Exit Sub
    
        Application.ScreenUpdating = False
        For Each xCell In xRg
            If Not Application.CheckSpelling(xCell.Text) Then _
                xCell.Interior.ColorIndex = 28
        Next xCell

        Application.ScreenUpdating = True
    
    Next ws
    
    MsgBox "Macro complete!"
    
End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,999
Messages
6,128,196
Members
449,432
Latest member
Novice Excel User

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