Clearing Cells VBA

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Hello all,
Here is my code to clear all my cells to have a blank template to begin again:

Code:
Sub ClearContents()
Dim sht As Worksheet
'In case of no numeric formula or constants
On Error Resume Next
'Procedure
For Each sht In ActiveWorkbook.Worksheets
    If (sht.Name <> "Instructions") And (sht.Name <> "Revisions") Then
    sht.Activate
        With Range("A8:AV2000")
            .SpecialCells(xlCellTypeConstants).ClearContents
            .SpecialCells(xlCellTypeFormulas).ClearContents
            .Interior.Pattern = xlNone
            Range("A8").Interior.ColorIndex = 6
            Range("B8").Interior.ColorIndex = 4
            .ColumnWidth = 8.14
        End With
    End If
Next sht
     
'Disables any error trapping currently present in the procedure
On Error GoTo 0

'Return to Template Tab
Range("A1").Select
Sheets("Template").Select
End Sub

The problem is column B retains a number format of "0.00" from when the rest of the program code is run. How can I get column B back to a "General" format?
I do want A8 and B8 to have color replaced, the reason why I have the color code after the special cells code.
Thanks
2013 excel
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try
Code:
        With Range("A8:AV2000")
            .ClearContents
            .NumberFormat = "General"
            .Interior.Pattern = xlNone
            Range("A8").Interior.ColorIndex = 6
            Range("B8").Interior.ColorIndex = 4
            .ColumnWidth = 8.14
        End With
 
Upvote 0
Ohh...that worked, and is cleaner. The only thing it did not clear, is a small range of border on cells that had a border from what was copied and pasted into the template.
is there code to clear borders from the sheet?
Thanks for the help

Of course, if I paste values only it does not have the borders
 
Last edited:
Upvote 0
Untested, but try changing
Code:
.ClearContents
to
.Clear
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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