vba to clear contents of the same range across multiple sheets

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
680
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I'm looking for some simple, efficient code that will clear the contents of the range A2:GR50000 across 50 worksheets.
I need to use sheet CodeNames in the vba.
Does anyone know what the fastest vba solution would be?
Thanks.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Why do we need to use Sheet code names?

Can we not do all sheets?
Or Sheets 1 to 50
Or Sheets 20 to 70
 
Upvote 0
so there are 50 worksheets you want changing, is this ALL worksheets in the workbook? If not, how many others are there? What's your reasoning that you need to use "sheet CodeNames"?
 
Upvote 0
It's going to be easier if the sheets are contiguous, or if you can create a list of sheet code names you want to clear, one idea though...

Code:
Option Explicit


Sub shshs()
Dim shCodes As String
Dim ws As Worksheet
shCodes = "|Sheet1|Sheet2|"         ' continue as required
For Each ws In ThisWorkbook.Worksheets
    If InStr(1, shCodes, "|" & ws.CodeName & "|") Then
        ws.Range("A2:GR50000").Clear
    End If
Next ws
End Sub
 
Upvote 0
Hi,
To confirm, no it's not all the sheets, there will be about 75 in the workbook ultimately.
Re. sheet codenames - scrub that! Don't know why I said that.
The sheets are all labelled 1-50 in the tabs at the bottom, sorry for incorrect info.
Hope this makes it easier
 
Upvote 0
Delete or comment out the one you don't want

Code:
Sub clearfifty()
Dim i As Long
For i = 1 To 50
    Sheets(i).Range("A2:GR50000").Clear
Next i
'' OR
For i = 1 To 50
    Sheets("SheetNamePrefix" & i).Range("A2:GR50000").Clear
Next i
End Sub
 
Upvote 0
Hi,

Have used the bottom code as I believe it's targetting the tab names which is what I need, but it's not playing. My sheet tab names are 1, 2, 3 etc..

Dim i As Long
For i = 1 To 50
Sheets("SheetNamePrefix" & i).Range("A2:GR50000").Clear
Next i
 
Upvote 0
Another possibility :

Code:
Sub v()
Dim x(1 To 50), i
For i = 1 To 50
    x(i) = "Sheet" & i
Next
Sheets("Sheet1").[A2:AR50000].ClearContents
Sheets(x).FillAcrossSheets Sheets("Sheet1").[A2:AR50000]
End Sub
 
Upvote 0
I see the problem in my code, try...

Code:
Sub clearfifty()
Dim i As Long
For i = 1 To 50
    If Not (IsError(Evaluate(CStr(i) & "!A1"))) Then
        Sheets(CStr(i)).Range("A2:GR50000").Clear
    End If
Next i
End Sub
 
Upvote 0
Hi,
The sheets aren't labelled Sheet1, Sheet2 ... they are labelled 1, 2 ...
 
Upvote 0

Forum statistics

Threads
1,216,081
Messages
6,128,696
Members
449,464
Latest member
againofsoul

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