Unable to replace zero date (0/1/1900)

hnd12000

New Member
Joined
Feb 10, 2019
Messages
22
Office Version
  1. 365
Platform
  1. Windows
I have cell with at date that should not exist that is 0/1/1900.
I'm trying to find a way in VBA to replace all such values by nothing.

I tried :
VBA Code:
Selection.Replace What:=0, Replacement:=""
Selection.Replace What:="0/1/1900", Replacement:=""
Selection.Replace What:=Format("0/1/1900", "D/M/YYYY"), Replacement:=""
Selection.Replace What:=Format(DateValue("0/1/1900"), "D/M/YYYY"), Replacement:=""
Selection.Replace What:=Format(DateValue(0), "D/M/YYYY"), Replacement:=""

The funny thing is that when you records it works and gives you
VBA Code:
    Cells.Replace What:="0/1/1900", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2

but when you replay the values are just never removed
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
if you treat it as the number it is it should be zero so LookAt:=xlWhole, should do that
 
Upvote 0
if you treat it as the number it is it should be zero so LookAt:=xlWhole, should do that
I thought the same thing, and tried it out, but it didn't for me either. I tried various different options, and couldn't get any to work.
Were you able to get it to work?
I wonder if it has something to do with VBA using an American version of Excel versus a European version. I know things something get a little "funny" when it comes to dates in VBA between the different versions.
 
Upvote 0
I have cell with at date that should not exist that is 0/1/1900.
I'm trying to find a way in VBA to replace all such values by nothing.

I tried :
VBA Code:
Selection.Replace What:=0, Replacement:=""
Selection.Replace What:="0/1/1900", Replacement:=""
Selection.Replace What:=Format("0/1/1900", "D/M/YYYY"), Replacement:=""
Selection.Replace What:=Format(DateValue("0/1/1900"), "D/M/YYYY"), Replacement:=""
Selection.Replace What:=Format(DateValue(0), "D/M/YYYY"), Replacement:=""

The funny thing is that when you records it works and gives you
VBA Code:
    Cells.Replace What:="0/1/1900", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2

but when you replay the values are just never removed
what if you cut it down to what you need

Cells.Replace What:="0/1/1900", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows
 
Upvote 0
Easiest option, if the dates do not have a time element, is probably like this
VBA Code:
With Selection
   .NumberFormat = "0"
   .Replace 0, "", xlWhole, , False, , False, False
   .NumberFormat = "dd/mm/yyyy"
End With
 
Upvote 0
@Fluff , I agree. The Replace method acts on strings and not on dates. In fact a date in Excel is a numeric value (with a date "tag"), although its appearance looks like a string, determined by the cell formatting. Change the cell formatting to number, general or text and then confirm that replacing a 0 with an empty string ("") will work.
 
Upvote 0
Interesting. On my American version, this did NOT work:
Cells.Replace What:="0/1/1900", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows

but this did:
With Selection .NumberFormat = "0" .Replace 0, "", xlWhole, , False, , False, False .NumberFormat = "dd/mm/yyyy" End With
 
Upvote 0
@Fluff , I agree. The Replace method acts on strings and not on dates. In fact a date in Excel is a numeric value (with a date "tag"), although its appearance looks like a string, determined by the cell formatting. Change the cell formatting to number, general or text and then confirm that replacing a 0 with an empty string ("") will work.
I know that the date is just a format, which is why I expected the "Replace 0" option to work, though it did not. Seems it wasn't quite that simple!
 
Upvote 0
I just hate doing anything with dates in VBA, as they're a right PITA. :mad:
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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