Convert text date to true date. vba help! Why this code doesn't work?

Xceller

Active Member
Joined
Aug 24, 2009
Messages
265
VBA gurus - I have some dates in text fomat on a worksheet that I need to convert them to true dates(Value). Here some sample of the text dates: '12/31/2011; Blank space12/31/2011. Thanks for your help in advance. Here is code:

Sub CnvtTextDate()

For Each Rng In Selection

Cells.Replace What:=" ", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

Cells.Replace What:=",", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

Trim (Rng)

With Rng

.NumberFormat = "mm/dd/yy;@"


End With

Next Rng

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
this works for me:
Code:
Sub CnvtTextDate()
    Dim rng As Variant
    For Each rng In Selection
        rng.Value = Trim(rng)
        rng.NumberFormat = "mm/dd/yy;@"
    Next rng
End Sub
 
Upvote 0
slightly confused. do u have a single date u need coverted or 2 dates in the same cell?
 
Upvote 0
Warship - I attached a command button to your code. For whatever reason I have to click the button 2 times to convert the dates to value dates. I tried stepping through the code, I think the 1st click it trims the the text string and the 2nd click converts the text string to value. Is there a way to do this in click of the button?
 
Upvote 0
I don't get that behavior with a Command Button.

The code is looping through the current selection.
Do you have anything else effecting the selection before/as it runs?

Maybe it would be better to set the range we look thru using a different method and not use Selection?
 
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,026
Members
449,061
Latest member
TheRealJoaquin

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