Help with formatting date

Versonol

New Member
Joined
Jul 10, 2022
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm working on a macro to scrape various news articles from various websites. Since they come from different sources, the formats for the dates are currently diverse. I would like to hard code the dates for certain sources into the following format dd/mm/yyyy Eg: 31/10/2022 .
I need assistance formatting the following dates :
Published November 1, 2022
Tue, Nov 01, 2022 - 09:58 PM
October 31, 2022 at 5:15 PM

I'm fine with either an excel function or vba code to format these dates.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
It would help if you provided information on your system's Regional Settings for dates and a sample with expected results.
N.B. You can post an extract of your information with the forum's tool named XL2BB.

T202211x.xlsm
ABC
1
2Published November 1, 20221-Nov-22
3October 31, 2022 at 5:15 PM31-Oct-2231-Oct-22 17:15
4Tue, Nov 01, 2022 - 09:58 PM1-Nov-22
5
6Published November 1, 20221-11-2022
7October 31, 2022 at 5:15 PM31-10-202231-10-2022 5:15 PM
8Tue, Nov 01, 2022 - 09:58 PM1-11-2022
9
3c
Cell Formulas
RangeFormula
C3,C7C3=ConvertToDate(TEXTBEFORE(A3," at"))+TIMEVALUE(TEXTAFTER(A3,"at ",1))
B2,B6B2=ConvertToDate(TEXTAFTER(A2," "))
B3,B7B3=ConvertToDate(TEXTBEFORE(A3," at"))
B4,B8B4=ConvertToDate(TEXTAFTER(TEXTBEFORE(A4," -"),","))



The UDF is fairly robust and I hesitate to guess about your system's setup.

UDF follows
VBA Code:
Function ConvertToDate(S As String)
If Not IsDate(S) Then
    ConvertToDate = CVErr(xlErrNA)
    Exit Function
End If
ConvertToDate = DateValue(S)
End Function
 
Upvote 0
Solution
It would help if you provided information on your system's Regional Settings for dates and a sample with expected results.
N.B. You can post an extract of your information with the forum's tool named XL2BB.

T202211x.xlsm
ABC
1
2Published November 1, 20221-Nov-22
3October 31, 2022 at 5:15 PM31-Oct-2231-Oct-22 17:15
4Tue, Nov 01, 2022 - 09:58 PM1-Nov-22
5
6Published November 1, 20221-11-2022
7October 31, 2022 at 5:15 PM31-10-202231-10-2022 5:15 PM
8Tue, Nov 01, 2022 - 09:58 PM1-11-2022
9
3c
Cell Formulas
RangeFormula
C3,C7C3=ConvertToDate(TEXTBEFORE(A3," at"))+TIMEVALUE(TEXTAFTER(A3,"at ",1))
B2,B6B2=ConvertToDate(TEXTAFTER(A2," "))
B3,B7B3=ConvertToDate(TEXTBEFORE(A3," at"))
B4,B8B4=ConvertToDate(TEXTAFTER(TEXTBEFORE(A4," -"),","))



The UDF is fairly robust and I hesitate to guess about your system's setup.

UDF follows
VBA Code:
Function ConvertToDate(S As String)
If Not IsDate(S) Then
    ConvertToDate = CVErr(xlErrNA)
    Exit Function
End If
ConvertToDate = DateValue(S)
End Function
Thank you! It works perfectly on Excel 365, however, when I use Excel 2016, it gives #VALUE! error instead of the dates.
 
Upvote 0
Your profile shows 365
The functions TextBefore and TextAfter are only in the latest version of Excel.
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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