VBA clear contents (number and date formats) but keep format

albertan

Board Regular
Joined
Nov 16, 2014
Messages
68
Office Version
  1. 365
Hello,
I appreciate if somebody please help me with how it's better to clear contents but preserve formatting.

I have a data with dates and numbers. When I use Sheet.Range ("A2").CurrentRegion.Delete this preserves formatting for numbers but does not preserve for dates.
When I use Sheet.Range ("A2").CurrentRegion.ClearContents then it preserves formatting for Dates but not for numbers.
Is there any other way to clear it so that I keep formatting for both dates and numbers?

Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try doing it this way...
Code:
Sheet.Range("A2").CurrentRegion.Value = ""
 
Last edited:
Upvote 0
Thank you. It only works for date format ranges but does not preserve format for numbered fields.

At this moment (as a temporary workaround) I'm just putting this code at the end of VBA to get back to number format:

Columns("L:R").NumberFormat = "General"
 
Upvote 0
Thank you. It only works for date format ranges but does not preserve format for numbered fields.
It does for me. If the date format was currency for a cell before assign "" to it and I then enter a number back into the cell, that number gets formatted as currency.... if the cell was formatted as a date before I assigned "" to it, then put a number (say 45678) into the cell, that number would be displayed with the date format of the previous date entry.



Columns("L:R").NumberFormat = "General"
I thought this is not what you wanted???
 
Upvote 0
Hi Rick, your code works for all columns that are formatted in Date and Text format but not for columns L through R which are formatted in number format.

That's why I had to put the code at the end to reformat columns L through R in a number format. (i.e. olumns("L:R").NumberFormat = "General")

 
Upvote 0
....but not for columns L through R which are formatted in number format.
What does happen for them? For example, if cell L2 is custom formatted as "$0.00" and set its value equal to "", then go back and enter 98.76, what displays in the cell?
 
Upvote 0
for columns L through R for some reason the cells are showing 1/1/1900 in format, which looks like a date format.

Even if I change these columns to "General" format and run the code, it changes it back to 1/1/1900 format.

In my code I'm bringing the data from the SQL server. I use SQL string and the source data is in a number format. But perhaps because I'm using a string in my SQL statement, the data is being imported in a text format and it overwrites the previous formatted data.

Thanks
 
Upvote 0
In my code I'm bringing the data from the SQL server. I use SQL string and the source data is in a number format. But perhaps because I'm using a string in my SQL statement, the data is being imported in a text format and it overwrites the previous formatted data.
I don't work with SQL, but as far as I know, importing data is the same as entering data and the cell adopts the format of the data being entered.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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