Edit every worksheet in a workbook

Uma Chauhan

New Member
Joined
Jul 30, 2019
Messages
7
Hi Guys,

I am trying to run a piece of code in all the sheets in a workbook. However my code is only editing first sheet and then courses through other sheets but does not edit them. PFB the code:

Sub Worksheet_Editor()



For Each Worksheet In ThisWorkbook.Sheets


Cells.Select
Selection.Replace What:="NA", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp


Next

ActiveWorkbook.Save


End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi Guys,

I am trying to run a piece of code in all the sheets in a workbook. However my code is only editing first sheet and then courses through other sheets but does not edit them. PFB the code:

Sub Worksheet_Editor()



For Each Worksheet In ThisWorkbook.Sheets


Cells.Select
Selection.Replace What:="NA", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp


Next

ActiveWorkbook.Save


End Sub

Try this.
Code:
Sub Worksheet_Editor()

Dim Worksheet As Worksheet

For Each Worksheet In ThisWorkbook.Sheets

    With Worksheet.UsedRange
    
        .Replace What:="NA", Replacement:="", LookAt:=xlWhole, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
            
        .SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
    
    End With

Next

ActiveWorkbook.Save

End Sub
 
Upvote 0
I got an error 1004 for the line :

.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp

There is nothing wrong with the actual code. The only problem is it is not going through all the sheets. I need it to run in all sheets in workbook
 
Upvote 0
I got an error 1004 for the line :

.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp

There is nothing wrong with the actual code. The only problem is it is not going through all the sheets. I need it to run in all sheets in workbook

I slightly edited your code to not require the use of Select. It works on my end so the error you are getting would just mean that there were no blank cells found. Place "on error resume next" before the loop starts.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,490
Members
448,967
Latest member
visheshkotha

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