Streamline quick VBA statement

mssbass

Active Member
Joined
Nov 14, 2002
Messages
253
Platform
  1. Windows
I have several worksheets in the same Excel file where I need to clear the contents in these specific columns (A,D,G,J,M,P,S & V) starting at row 2 in each column. My worksheets are named (8am, 12pm, 2pm, 4pm and 6pm) - Here is the code so far that clears these contents but it seems like I could add a "For each" statement and ranges in order to streamline this statement and go through each worksheet - any help would be greatly appreciated!

Sub CleanFile()
Dim wsDest As Worksheet
Dim lDestLastRow As Long

Set wsDest = Workbooks("Staffing Bucket Template.xlsm").Worksheets("8am")
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
lDestLastRow2 = wsDest.Cells(wsDest.Rows.Count, "D").End(xlUp).Offset(1).Row
lDestLastRow3 = wsDest.Cells(wsDest.Rows.Count, "G").End(xlUp).Offset(1).Row
lDestLastRow4 = wsDest.Cells(wsDest.Rows.Count, "J").End(xlUp).Offset(1).Row
lDestLastRow5 = wsDest.Cells(wsDest.Rows.Count, "M").End(xlUp).Offset(1).Row
lDestLastRow6 = wsDest.Cells(wsDest.Rows.Count, "P").End(xlUp).Offset(1).Row
lDestLastRow7 = wsDest.Cells(wsDest.Rows.Count, "S").End(xlUp).Offset(1).Row
lDestLastRow8 = wsDest.Cells(wsDest.Rows.Count, "V").End(xlUp).Offset(1).Row
'Clear contents of existing data range
wsDest.Range("A3:A" & lDestLastRow).ClearContents
wsDest.Range("D3:D" & lDestLastRow2).ClearContents
wsDest.Range("G3:G" & lDestLastRow3).ClearContents
wsDest.Range("J3:J" & lDestLastRow4).ClearContents
wsDest.Range("M3:M" & lDestLastRow5).ClearContents
wsDest.Range("P3:P" & lDestLastRow6).ClearContents
wsDest.Range("S3:S" & lDestLastRow7).ClearContents
wsDest.Range("V3:V" & lDestLastRow8).ClearContents

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
How about
Code:
Sub mssbass()
   Dim Ws As Worksheet
   
   For Each Ws In Worksheets(Array("8am", "12pm", "2pm", "4pm", "6pm"))
      Intersect(Ws.UsedRange.Offset(1), Ws.Range("A:A,D:D,G:G,J:J,M:M,P:P,S:S,V:V")).ClearContents
   Next Ws
End Sub
This clears from row 2 as per your description, rather than row 3 as per your code.
 
Last edited:
Upvote 0
That's what the code does, it will clear everything in those columns with the exception of row 1
 
Upvote 0
My pleasure & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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