Need help running code in all the worksheets except 4 of then out of 12. Thanks

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
I have code that was shared by Peter, but i want the code to run on all the worksheets except on Sheets("KData", "HGData","GData","VData")


Thanks for helping!

Code:
Sub try1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Home" Then
    'mycode
    End If
Next ws
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Here's one way it could be done...

Code:
Sub try1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    [COLOR="Red"]If InStr("KData HGData GData VData", ws.Name) = 0 Then[/COLOR]
    'mycode
    End If
Next ws
End Sub
 
Upvote 0
As another option you could go with...

Code:
Sub try1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets(Array("Sheet1", "Sheet3"))
     'mycode
Next ws
End Sub
 
Upvote 0
I tried this way hoping that if sheet.name <> any of these 4 sheets names ("KData", "HGData","GData","VData") then run the code in it...

I want the code to run on all sheets except this four...
I hope i am doing it right but it is not working so far:)
Code:
Sub rtry1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    If InStr("KData HGData GData VData", ws.Name) = 0 Then
    'mycode
    Cells(1).Value = "Hi"
    End If
Next ws
End Sub
 
Upvote 0
My ignorance sorry..I tried coding it this way and it worked now :)

Thanks alot!

Have a great weekend!
Code:
Sub rtry1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    If InStr("KData HGData GData VData", ws.Name) = 0 Then
    'mycode
    ws.Cells(1).Value = "Hi"
    End If
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,806
Members
449,048
Latest member
greyangel23

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