Macro to clear Values Only

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,186
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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Colo

MrExcel MVP,
Joined
Mar 20, 2002
Messages
1,659
Office Version
  1. 2016
  2. 2003 or older
Platform
  1. Windows
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
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,186
Office Version
  1. 2021
Platform
  1. Windows
Thanks for the reply

My apologies. I only want to clear values but no formulas
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,852
Office Version
  1. 2016
Platform
  1. Windows
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
 
Solution

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,186
Office Version
  1. 2021
Platform
  1. Windows
Thanks Jafaar

Your code works perfectly
 

Forum statistics

Threads
1,176,168
Messages
5,901,734
Members
434,914
Latest member
mabek

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
Top