VBA to update all worksheets except one

cab401

New Member
Joined
Jun 14, 2018
Messages
13
I need to clear specific cells & unmerge 2 cells on every worksheet except Sheet1.

Below is the code I'm using, however, after updating all pages appropriately, it updates Sheet1 as well. Help! VBA newbie . . .

Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then

Range("G5").Select
Selection.ClearContents
Range("I8:J8").Select
Selection.ClearContents
Range("D14:E14").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
Range("F4").Select
End If
Next ws

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
The issue is that looping through the names of the sheets does not actually activate them.
So all ranges that do have a sheet component default to the active sheet.

So, you need to do one of three things after your "IF" statement.

1. Select/activate the sheet:
Code:
ws.Activate

2. Use a sheet reference in front of each range, i.e.
Code:
[COLOR=#333333]ws.Range("G5").ClearContents[/COLOR]

3. Use a With statement, and put periods if front of all your range to indicate that it applies to that sheet, i.e.
Code:
With ws
[COLOR=#333333]    .Range("G5").ClearContents
[/COLOR]    ...
End With

Also, you can get rid of most of your "Select" statements. It is not necessary to select ranges in order to work with them. Most lines that end with "Select" and the next row begins with "Selection" can be combined into one line.

Try this updated code:
Code:
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Sheet1" Then
            With ws
                .Range("G5").ClearContents
                .Range("I8:J8").ClearContents
                With .Range("D14:E14")
                    .HorizontalAlignment = xlGeneral
                    .VerticalAlignment = xlBottom
                    .WrapText = False
                    .Orientation = 0
                    .AddIndent = False
                    .IndentLevel = 0
                    .ShrinkToFit = False
                    .ReadingOrder = xlContext
                    .MergeCells = True
                End With
                .Range("D14:E14").UnMerge
            End With
        End If
    Next ws
 
Upvote 0
This successfully unmerged the 2 cells but did not clear the contents from G5 or I8:J8.
It did for me.

Did you copy my code, EXACTLY as I have written it?
What is the name of the Module where you have this VBA code in?
Do you have any other VBA code in your workbook that may be interfering with it?
 
Last edited:
Upvote 0
Joe, sorry for the confusion - I figured out what I did wrong. Your code is working PERFECTLY!

Thank you very much!!
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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