Looping through various worksheets VBA

mwhite1998

New Member
Joined
Nov 21, 2023
Messages
3
Office Version
  1. 365
Hello,

I am looking to create a loop to run through all the worksheets in my workbook to run some code that clears the contents of "blank cells".

The code works for the active worksheet but doesn't seem to run through the loop to the next one.
VBA Code:
Sub WorksheetLoop()
Dim ws As Worksheet

For Each ws In Worksheets

Call RemoveBlanks

Next

End Sub

Sub RemoveBlanks()

columnNumber = 1
Do Until columnNumber > 26
    rowNumber = 2
    
    columnLetter = Chr(columnNumber + 64)
    lastRow = Range(columnLetter & rows.Count).End(xlUp).Row
    Do Until rowNumber > lastRow
        If Range(columnLetter & rowNumber).Value = "" Then
            Range(columnLetter & rowNumber).ClearContents
        End If
        rowNumber = rowNumber + 1
    Loop
    columnNumber = columnNumber + 1
Loop


           
End Sub

Thanks for any help :)

Matt
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Matt, maybe

change
VBA Code:
For Each ws In Worksheets

to
VBA Code:
For Each ws In ActiveWorkbook.Worksheets
 
Upvote 0
It works for me.

Try this. When I tested like this, every sheet is called.

VBA Code:
Sub WorksheetLoop()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Call RemoveBlanks
    Next
End Sub
 
Upvote 1
Solution
It works for me.

Try this. When I tested like this, every sheet is called.

VBA Code:
Sub WorksheetLoop()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Call RemoveBlanks
    Next
End Sub
This one worked! Must be the ws.activate?

Either way, thanks!
 
Upvote 0
The main reason it was not working on all worksheets was because the code did not know to move from sheet to sheet.

Example:
VBA Code:
lastRow = Range(columnLetter & rows.Count).End(xlUp).Row

The code didn't have a way to know that it needs to find the lastrow on any other worksheet than the active one.

The best way to handle this is using the ws.activate or simply building the code within the first code versus calling it. So again, an example. The line above would be

lastRow = ws.Range(columnLetter & rows.Count).End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,215,106
Messages
6,123,120
Members
449,096
Latest member
provoking

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