Deleting values on various sheets

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,199
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I have a command button of which when run it will delete certain cell value on different worksheets.
The worksheets are all in the same workbook called ACCOUNTS

The below code is what ive done so far BUT im stuck with the code that fits where you see CODE GOES HERE
The part which i always get wrong where to tell the code to plly the deletion to what sheet



Rich (BB code):
Private Sub ClearSheetValues_Click()
Dim answer As Integer
answer = MsgBox("THIS WILL CLEAR ALL CELL VALUES" & vbNewLine & vbNewLine & "ON ALL WORKSHEETS" & vbNewLine & vbNewLine & "CLICK YES TO CONTINUE", vbYesNo + vbCritical, "ACCOUNTS CLEAR VALUES MESSAGE")
If answer = vbNo Then
  Exit Sub
Else
End If

CODE TO GO HERE

End Sub

REFERENCE BELOW FOR WHICH SHEETS / CELLS NEED TO BE DELETED

INCOME (1}

Range("A4:G28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents

INCOME (2)

Range("A5:G28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("C4:G4").SpecialCells(xlCellTypeConstants).ClearContents

INCOME (3)

Range("A5:G28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("C4:G4").SpecialCells(xlCellTypeConstants).ClearContents

EXPENSES (1)

Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("A4:K28").SpecialCells(xlCellTypeConstants).ClearContents

EXPENSES (2)

Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents

EXPENSES (8)
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents

mileage (1)

Range("B1:D1").SpecialCells(xlCellTypeConstants).ClearContents
Range("A3:D29").SpecialCells(xlCellTypeConstants).ClearContents
Range("A34").SpecialCells(xlCellTypeConstants).ClearContents
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Like so,
RTE( subscript out of range ??

Rich (BB code):
Private Sub ClearSheetValues_Click()
Dim answer As Integer
answer = MsgBox("THIS WILL CLEAR ALL CELL VALUES" & vbNewLine & vbNewLine & "ON ALL WORKSHEETS" & vbNewLine & vbNewLine & "CLICK YES TO CONTINUE", vbYesNo + vbCritical, "ACCOUNTS CLEAR VALUES MESSAGE")
If answer = vbNo Then
Exit Sub

Else
End If
With ThisWorkbook.Sheets("INCOME(1)")
Range("A4:G28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("INCOME(2)")
Range("A5:G28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("C4:G4").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("INCOME(3)")
Range("A5:G28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("C4:G4").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("EXPENSES(1)")
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("A4:K28").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("EXPENSES(2)")
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("EXPENSES(3)")
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("EXPENSES(4)")
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("EXPENSES(5)")
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("EXPENSES(6)")
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("EXPENSES(7)")
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("EXPENSES(8)")
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
End With

With ThisWorkbook.Sheets("MILEAGE")
Range("B1:D1").SpecialCells(xlCellTypeConstants).ClearContents
Range("A3:D29").SpecialCells(xlCellTypeConstants).ClearContents
Range("A34").SpecialCells(xlCellTypeConstants).ClearContents
End With

End Sub
 
Upvote 0
The above issue has now been fixed, space between word & (1) etc
Now when i run the code i see the RTE 1004 No cells were found.
I debug and see the following line highlied.

Im now not sure how to fix this as screenshot shows values are there ??

Code at present shown below


Rich (BB code):
Private Sub ClearSheetValues_Click()
Dim answer As Integer
answer = MsgBox("THIS WILL CLEAR ALL CELL VALUES" & vbNewLine & vbNewLine & "ON ALL WORKSHEETS" & vbNewLine & vbNewLine & "CLICK YES TO CONTINUE", vbYesNo + vbCritical, "ACCOUNTS CLEAR VALUES MESSAGE")
If answer = vbNo Then
Exit Sub

Else
End If
With ThisWorkbook.Sheets("INCOME (1)")
Application.EnableEvents = False
Range("A4:G28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("INCOME (2)")
Application.EnableEvents = False
Range("A5:G28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("C4:G4").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("INCOME (3)")
Application.EnableEvents = False
Range("A5:G28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("C4:G4").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("EXPENSES (1)")
Application.EnableEvents = False
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("A4:K28").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("EXPENSES (2)")
Application.EnableEvents = False
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("EXPENSES (3)")
Application.EnableEvents = False
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("EXPENSES (4)")
Application.EnableEvents = False
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("EXPENSES (5)")
Application.EnableEvents = False
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("EXPENSES (6)")
Application.EnableEvents = False
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("EXPENSES (7)")
Application.EnableEvents = False
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("EXPENSES( 8)")
Application.EnableEvents = False
Range("A5:K28").SpecialCells(xlCellTypeConstants).ClearContents
Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Range("D4:K4").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

With ThisWorkbook.Sheets("MILEAGE")
Application.EnableEvents = False
Range("B1:D1").SpecialCells(xlCellTypeConstants).ClearContents
Range("A3:D29").SpecialCells(xlCellTypeConstants).ClearContents
Range("A34").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With

End Sub
 

Attachments

  • 189.jpg
    189.jpg
    21.4 KB · Views: 3
  • 190.jpg
    190.jpg
    102.2 KB · Views: 3
Upvote 0
Do the cells definitely contain constants and not formulas? asking because column B definitely looks like formulas.
 
Upvote 0
At the moment your with statements are doing nothing & those ranges are all looking at the active sheet.
You need to put a full stop before the range like
VBA Code:
With ThisWorkbook.Sheets("INCOME (1)")
Application.EnableEvents = False
.Range("A4:G28").SpecialCells(xlCellTypeConstants).ClearContents
.Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
End With
 
Upvote 0
Do the cells definitely contain constants and not formulas? asking because column B definitely looks like formulas.

Column E are IF statements.
Row 30 & 32 are =SUM formulas
Other than that just numbers / letters

@Fluff I have now added the full stop as advised & things now are moving along with respect of deleting values.
There is one thing id like to ask.

Say INCOME (2) doesnt have any values, The code still tries to delete something that isnt there & thus pops up a RTE1004
How can i supress this error & just continue with the code.

Thanks
 
Upvote 0
How about
VBA Code:
With ThisWorkbook.Sheets("INCOME (1)")
Application.EnableEvents = False
On Error Resume Next
.Range("A4:G28").SpecialCells(xlCellTypeConstants).ClearContents
.Range("B1:C1").SpecialCells(xlCellTypeConstants).ClearContents
On Error GoTo 0
Application.EnableEvents = True
End With
 
Upvote 0
Did you add those two lines to each of your with statements?
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,177
Members
448,554
Latest member
Gleisner2

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