how to use macro for different sheets

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
Office Version
  1. 2019
Platform
  1. Windows
Hello. I have here a code which only runs on the active sheet but I need it to run for all the sheets except one. How to modify this in order for it to run through all worksheets?

VBA Code:
Sub DelData ()
Dim ws as Worksheet

For Each ws In ThisWorkbook.Worksheets
If ws.Name = "SourceData" Then
Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Delete
Next ws

End If

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
which worksheet do you NOT want to run the code on ?
 
Upvote 0
AND you are just clearing ALL the data in col "A" on the sheet ??
 
Upvote 0
Try it this way....assuming the "Source Data" sheet is the one to skip
VBA Code:
Sub DelData()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name <> "SourceData" Then ws.Range("A:A").Clear
Next ws
End Sub
 
Upvote 0
It should be "A:AA"

I've tried your code but still it only runs on the active worksheet? what could be the problem?
 
Upvote 0
It should be "A:AA"
...."A:A" will clear column "A" only, Do you mean you want more columns cleared ?
I've tried your code but still it only runs on the active worksheet? what could be the problem?
Where are you pasting the code....it works fine for me....Which sheet is the one to skip ?
 
Upvote 0
Are any of the sheets protected ??
 
Upvote 0
Hi I tried again the code you provided and it works. Maybe I made some changes on it earlier ?

I just want to confirm why it runs although it doesn't have "End If" for this Line "If ws.Name <> "SourceData" Then ws.Range("A:A").Clear"
 
Upvote 0
Hello. I also made some changes on my code earlier. What I need is to delete all contents of columns A to AA for all the worksheets other than "SourceData".
However, my code below doesn't still run properly.


VBA Code:
Sub DelData ()
Dim ws as Worksheet

For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "SourceData" Then
Range("A1:AA" & Range("A" & Rows.Count).End(xlUp).Row).Delete
Next ws

End If

End Sub
 
Upvote 0
Change the Range("A1:AA").....by removing the digit
Change the word "delete" to "clear"....if you don't need to remove the columns
 
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,576
Members
449,318
Latest member
Son Raphon

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