Delete date in 3 sheets once we run the macros

An Quala

Board Regular
Joined
Mar 21, 2022
Messages
146
Office Version
  1. 2021
Platform
  1. Windows
Hello, can anyone please write me a code which deletes the date in the 3 sheets present in same workbook, the sheet names are "Sheet1", "Sheet2" and "Sheet3". Please note that only data needs to be deleted, not the sheets themselves.

Thank you.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Do you want to leave formatting, comments, hyperlinks, notes and outlines on the sheets?

This code will just remove the contents.

Test it a copy of your data.

VBA Code:
Private Sub subClearContentsFromSheets()
Dim i As Integer

    For i = 1 to 3
        Worksheets("Sheet" & i).Cells.ClearContents
    Next i
    
End Sub

This code will clear everything.

VBA Code:
Private Sub subClearAllSheets()
Dim i As Integer

    For i = 1 to 3
        Worksheets("Sheet" & i).Cells.Clear
    Next i
    
End Sub
 
Upvote 0
Private Sub subClearAllSheets() Dim i As Integer For i = 1 to 3 Worksheets("Sheet" & i).Cells.Clear Next i End Sub
Thank you, one issue is that I used the name of the sheets as an example because they changes with time as well, and I am able to change them myself, but here in your code, I could not do it. The name of the sheets could be "Sponsored Product Campaigns", "Sponsored Brands Campaigns" and "Sponsored Display Campaigns", can you please update the code 2 accordingly?

Do you want to leave formatting, comments, hyperlinks, notes and outlines on the sheets?
Yes, I need to erase everything including formatting.

Thank you @Herakles
 
Upvote 0
This will do it.

If I was doing this I would have a Yes/No prompt before the sheets were cleared.
I'd maybe even copy the sheets to another workbook first.

VBA Code:
Private Sub subClearAllFromSheets()
Dim i As Integer
Dim strSheets As String
Dim arr() As String
    
    ' Sheet names are seperated by a comma.
    strSheets = "Sponsored Product Campaigns,Sponsored Brands Campaigns,Sponsored Display Campaigns"

    arr = Split(strSheets, ",")
    
    For i = LBound(arr) To UBound(arr)
        Worksheets(arr(i)).Cells.Clear
    Next i
    
End Sub
 
Upvote 1
Solution
Has it cleared any of the sheets?
When runnning the macro directly, it did not clear any sheet and gave the error. But when I try to debug the code and did it step by step with F8, it cleared all 3 sheets but still goes on and on in loop, and I think this was the problem with runtimeerror 9.
 
Upvote 0
That is weird.

The Split function should only create an array of 3 elements.

The loop should just cover the first through to the last of these.
 
Upvote 0
That is weird.

The Split function should only create an array of 3 elements.

The loop should just cover the first through to the last of these.
It is working perfectly, exactly as I needed.

Thank you very much for the quick help!
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,653
Members
452,992
Latest member
TokugawaIesuma

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