Macro to clear Values Only

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have tried to write code to clear Non-Formulas, but also do not want any tect cleared i.e I only want Values clear on sheet ("JNLS")

Kindly amend my code below

Code:
Sub ClearDataNonFormulas()
With Sheets("INLS")

With CreateObject("VBScript.RegExp")
On Error Resume Next

Sheets(1).UsedRange.SpecialCells(xlCellTypeConstants, 1).ClearContents
     End With
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Do you mean that you don't want to clear any Texts being shown by Formulas, but would like to clear all the Values? My idea is like this. As the first step, preserving ranges has Values using the "Range.SpecialCells" method. Then after converting the results of formulas to Values, Clear preserved ranges.

VBA Code:
Sub test()
Sub ClearDataNonFormulas()
    Dim rngPreserved As Range
    With Sheets("INLS")
        Set rngPreserved = .UsedRange.SpecialCells(xlCellTypeConstants, 1)
        .UsedRange.Value = .UsedRange.Value
        rngPreserved.Clear
    End With
End Sub
 
Upvote 0
Thanks for the reply

My apologies. I only want to clear values but no formulas
 
Upvote 0
Is this what you want ?
Caution: Try the code on a mock worksheet first because it will delete the numeric cells contents and you won't be able to undo it.

VBA Code:
On Error Resume Next
Sheets("JNLS").UsedRange.SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents
 
Upvote 0
Solution

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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