VBA to clear cell contents and conditional formatting but retain the formula

MLC1980

New Member
Joined
Oct 1, 2024
Messages
13
Office Version
  1. 2016
Platform
  1. Windows
Hi

I have a 'Reset' button on my Excel worksheet that uses the following VBA:

VBA Code:
Sub ResetSPLPlanner()
'
' ResetSPLPlanner Macro
'

'
    ActiveSheet.Protect UserInterFaceOnly:=True
    Range("D13:E64").Select
    Selection.ClearContents
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    Range("H1:H2").Select
    Selection.ClearContents
    
    End With
    Range("A1").Select
End Sub

When I first wrote the above code, the cells that I was clearing did not contain any formulas so it worked fine. However, I have now added some formula to cells D13:D64. It's worth noting that apart from the new formula to cells D13:D64, the worksheet has all the other formula cells within the worksheet locked (I do not have the formula in cells D13:D64 locked because the user still needs to be able to overwrite it).

What I need the Reset button to do now is:
Allow the VBA to turn on a protected worksheet
For Cells D13:D64 - clear the content whilst retaining the formula, set the fill colour to xlNone
For Cells E13:E64 - clear the content and set the fill colour to xlNone
For Cells H1:H2 - clear the contents

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Just posting again to correct a spelling mistake
Hi

I have a 'Reset' button on my Excel worksheet that uses the following VBA:

VBA Code:
Sub ResetSPLPlanner()
'
' ResetSPLPlanner Macro
'

'
    ActiveSheet.Protect UserInterFaceOnly:=True
    Range("D13:E64").Select
    Selection.ClearContents
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    Range("H1:H2").Select
    Selection.ClearContents
   
    End With
    Range("A1").Select
End Sub

When I first wrote the above code, the cells that I was clearing did not contain any formulas so it worked fine. However, I have now added some formula to cells D13:D64. It's worth noting that apart from the new formula to cells D13:D64, the worksheet has all the other formula cells within the worksheet locked (I do not have the formula in cells D13:D64 locked because the user still needs to be able to overwrite it).

What I need the Reset button to do now is:
Allow the VBA to run on a protected worksheet
For Cells D13:D64 - clear the content whilst retaining the formula, set the fill colour to xlNone
For Cells E13:E64 - clear the content and set the fill colour to xlNone
For Cells H1:H2 - clear the contents

Thanks
 
Upvote 0
Your text does not mention conditional formatting.
Ignoring conditional formatting this might work for you.
It won't clear content of cells that the user entered starting with "=" ie adding up figures in the cell.

VBA Code:
Sub ResetSPLPlanner()
'
' ResetSPLPlanner Macro
'
    ActiveSheet.Protect UserInterFaceOnly:=True
    Range("D13:D64").SpecialCells(xlCellTypeConstants, 23).ClearContents
    Range("E13:E64").ClearContents
    
    With Range("D13:E64").Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

    Range("H1:H2").ClearContents
    
    Range("A1").Select
End Sub
 
Upvote 0
@Alex Blakenburg

Thanks for your reply, I do have the following conditional formatting applied to cells D13:D64
1730115324832.png


I tried the code you provided but got run-time error 1004, no cells were found
 
Upvote 0
You must not have had any cells that did not have formulas, we will need to add error trapping for that scenario.
Also to remove conditional formatting the sheet protection needs to have been removed.

Try the below:

VBA Code:
Sub ResetSPLPlanner()
'
' ResetSPLPlanner Macro
'
    ActiveSheet.Unprotect
    
    On Error Resume Next
    ' if it errors out all the cells are formulas - so do nothing ie do not clear contents
    Range("D13:D64").SpecialCells(xlCellTypeConstants, 23).ClearContents
    On Error GoTo 0
    Range("E13:E64").ClearContents
    
    With Range("D13:E64")
        .Cells.FormatConditions.Delete              ' Need sheet protection removed for this to work
        With .Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End With

    Range("H1:H2").ClearContents
    
    Range("A1").Select
    
    ActiveSheet.Protect UserInterFaceOnly:=True
End Sub
 
Upvote 0
You must not have had any cells that did not have formulas, we will need to add error trapping for that scenario.
Also to remove conditional formatting the sheet protection needs to have been removed.

Try the below:

VBA Code:
Sub ResetSPLPlanner()
'
' ResetSPLPlanner Macro
'
    ActiveSheet.Unprotect
   
    On Error Resume Next
    ' if it errors out all the cells are formulas - so do nothing ie do not clear contents
    Range("D13:D64").SpecialCells(xlCellTypeConstants, 23).ClearContents
    On Error GoTo 0
    Range("E13:E64").ClearContents
   
    With Range("D13:E64")
        .Cells.FormatConditions.Delete              ' Need sheet protection removed for this to work
        With .Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End With

    Range("H1:H2").ClearContents
   
    Range("A1").Select
   
    ActiveSheet.Protect UserInterFaceOnly:=True
End Sub
@Alex Blakenburg the problem I had with that is it deleted the conditional formatting for the next user, I still needed the conditional formatting to work for the next user.
I managed to resolve it by changing:
VBA Code:
.cells.FormatConditions.Delete
to
VBA Code:
.cells.FormatConditions.AddUniqueValues

Thanks for your help.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,259
Messages
6,177,485
Members
452,782
Latest member
ZCapitao

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