Date Formatting Function

AndrewKent

Well-known Member
Joined
Jul 26, 2006
Messages
889
Hi there,

I am extracting data from a website and I have a text string that represents a date:

"Friday, August 6, 2010"

It will always be returned in this format and I want to write a formula that will convert this to:

"06/08/2010"

which I can then use.

Any suggestions?

Andy
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I was able to do a text->columns, concatenate the Month/day/year, convert to date value, and then format it.

Is this too roundabout? I don't know how to do it directly in one step.
 
Last edited:
Upvote 0
Andy

There are lots of way you could do that.

For example:

If you remove the day and first comma VBA will recognise that as a date.

This code is just to demonstrate that.
Code:
Dim strDate As String
 
strDate = "Friday, August 6, 2010"
 
strDate = Mid(strDate, InStr(strDate, ",") + 1)

Debug.Print IsDate(strDate)
 
strDate = Format(strDate, "dd/mm/yyyy")
 
Debug.Print strDate
 
Upvote 0
My solution certainly isn't the shortest route, but it worked for me. The only thing you need to change is the RANGE reference that tells the macro what your source range is..

Code:
Sub ChangeDate()
Dim OrigTxtDate As String
Dim FormattedDate As String
Dim Month As String
Dim dte As Date
Dim Day As String
Dim Year As String
Dim myRange As Range
Dim NewDate As Date
 
On Error Resume Next
 
Dim s1 As Integer, s2 As Integer, s3 As Integer, s4 As Integer
 
Set myRange = Sheet1.Range("A1:A5") 'Set your range here.
 
For Each cell In myRange
OrigTxtDate = cell.Text
 
s1 = Application.WorksheetFunction.Search(" ", OrigTxtDate, 1) 'Locate first space
s2 = Application.WorksheetFunction.Search(" ", OrigTxtDate, s1 + 1) 'Locate second space
s3 = Application.WorksheetFunction.Search(" ", OrigTxtDate, s2 + 1) 'Locate third space
s4 = Application.WorksheetFunction.Search(",", OrigTxtDate, s1) 'Locate comma
 
Month = Mid(OrigTxtDate, s1 + 1, (s2 - s1) - 1)
Day = Mid(OrigTxtDate, s2 + 1, s4 - (s2 + 1))
Year = Right(OrigTxtDate, 4)
 
FormattedDate = convertMonthName2Number(Month) & "/" & Day & "/" & Year
 
NewDate = CDate(FormattedDate)
 
cell.Value = NewDate
 
Next
 
End Sub
 
 
Function convertMonthName2Number(monthName As String) As Integer
 
Dim dtestr As String
 
dtestr = monthName & "/1/2000"
 
Dim dte As Date
 
On Error Resume Next
 
dte = CDate(dtestr)
 
If Err.Number <> 0 Then
  convertMonthName2Number = -999
  Exit Function
End If
 
On Error GoTo 0
 
convertMonthName2Number = Month(dte)
 
End Function
 
Upvote 0
SCal

You could shorten your code using the VBA InStr function.

I actually think there's a whole load of ways this could be done.
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,920
Latest member
jaspers

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