Hi all
I am trying to build a VBA code that will clear the contents of select cells in all the sheets in my workbook, with the exception of one sheet.
The workbook has 54 sheets. The first sheet is named "Intro" and the remaining sheets are name 1 to 53, representing weeks.
The cells of these sheets I want to clear are, C5:G46.
I want to clear the contents of each sheet, activate cell C5 as the active cell, then move on the the next until the workbook is clear.
Ultimately, I want to be able to:
Below is my code. The error I get is a 1004 for the "Range" part of the code.
Please can you help?
I am trying to build a VBA code that will clear the contents of select cells in all the sheets in my workbook, with the exception of one sheet.
The workbook has 54 sheets. The first sheet is named "Intro" and the remaining sheets are name 1 to 53, representing weeks.
The cells of these sheets I want to clear are, C5:G46.
I want to clear the contents of each sheet, activate cell C5 as the active cell, then move on the the next until the workbook is clear.
Ultimately, I want to be able to:
- Save the file as a new file with a new filename (The year, plus one - so if the sheet is 2022.xlsm, then the new sheet is 2023.xlsm)
- Change cell C2 on the Intro sheet to the next year (so if it says 2022, then it is changed to 2023)
- Clear the contents of the cell ranges in the remaining sheets
- Save the document to apply the changes
Below is my code. The error I get is a 1004 for the "Range" part of the code.
Please can you help?
VBA Code:
Private Sub CommandButton1_Click()
' SelectCellsClear Macro
' Selects a range of cells in all sheets except Intro & Settings, then clears their contents
Dim a As Integer
numsheets = Application.Worksheets.Count 'Count the number of worksheets in the workbook and assign this to the integer
For ws = 1 To numsheets 'Defines ws as a counter starting at 1 and ending o nthe number of sheets
Worksheets(ws).Activate 'Makes the sheet active
If ActiveSheet.Name <> "Intro" Then 'This sheet is ignored from the loop
Range("C5:G46").Select 'This the range of cells to selected
Selection.Clear 'Clear the contents of the selectd cells
Range("B2").Select 'Make cell C5 the active cell before moving on
End If
Next 'Repeat this for all the sheets
Worksheets("Intro").Activate 'Go back to the first sheet
Worksheets("Intro").Cells(1, 1).Select 'Make cell A1 the active cell
End Sub