Cannot replace accidental column date-values

cfp999

New Member
Joined
Jun 23, 2009
Messages
8
Hi,


I have a simple Excel-sheet with some data from an SQL-export. Row B contains information on your typical age-intervals (0 - 9, 10 - 19, 20 - 29, 30 - 39....). The intervals are formatted as text. Unfortunately, Excel automatically converts the interval "10 - 19" to a date, Oct-19.


0 - 9
Oct-19
20 - 29
30 - 39
40 - 49
50 - 59
60 - 69
70- 79
80 - 89


When I examine these cells I notice that the original information is lost - formatting the cells back to "General" results in a date number = 43739.


I want to find and replace all instances of "Oct-19" in column B, with "'10 - 19" using VBA. This works perfectly well when done manually. I have recorded the steps, but for some reason, this does nothing when executed as code:


Code:
Columns("B:B").Select    Selection.Replace What:="01-10-2019", Replacement:="'10 - 19", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False


I have tried replacing What:="01-10-2019" with "Oct-19". Also tried adding a format, What:=Format("01-10-2019", "dd/mm/yyyy"). Still nothing. I'm missing something here?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try:
Code:
Sub test()
    Columns("B:B").Replace What:="10/19/2019", Replacement:="'10 - 19", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
End Sub
 
Last edited:
Upvote 0
How about
Code:
What:=CDate("01-10-2019")
 
Upvote 0
Thank you, but I still get 'Oct-19'. May I should try forcing a general number format to the cells, and then identify them as 43739?
 
Upvote 0
Just a suggestion as I haven't tried it out, but try formatting Column B (your age-bands column) as Text BEFORE running your code.
 
Last edited:
Upvote 0
Have you tried the macro I suggested? It worked on the data that you posted.
 
Upvote 0
Thank you, but I still get 'Oct-19'. May I should try forcing a general number format to the cells, and then identify them as 43739?

Is that with the code mumps supplied, or the change I suggested?
 
Upvote 0
Hi, I tried both yours, and the code suggested by mumps. Since mumps got it to work, I think my problem may be related to the way my data is formatted. The age-intervals are "General", while the oct-19 is "Userdefined" mmm-yy. If I manually change the format of the column to "Text", the oct-19 entries changes to 43739. Then I am able to replace that number with 10 - 19:

Code:
Columns("B:B").Replace What:="43739", Replacement:="'10 - 19", LookAt:= _    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

However, for some reason, I cannot change the format of the column in code, only by doing it manually.
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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