Extract date from Text

Gregfox

Board Regular
Joined
Apr 12, 2011
Messages
120
Hi, I have the following: 01/10/2021 5:37pm ET This of course changes every day, both the time and date and year.
I'm looking for VBA code to extract only the date(in this case 01/10/2021) and be able to cope with the changing dates/time/year.
I've tried lots of different code but can't seem to get it.
I have this: "01/10/2021 5:37pm ET" BUT need this: "01/10/2021"

Thanks!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
If the format is always the same, try:
Excel Formula:
=LEFT(A1,FIND(" ",A1)-1)
 
Upvote 0
VBA:

VBA Code:
Dim tx As String
tx = Left(Range("A1").Text, 10)
 
Upvote 0
Solution
Hi,

Assuming the content is in cell C3.

VBA Code:
Sub myMacro()
    Dim myDate As String
    myDate = Left(ActiveSheet.Range("C3"), 10)
    msgbox myDate
    MsgBox Format(myDate, "DD-MMM-YY")

End Sub
 
Upvote 0
It would be useful to know if the data is Text or real Dates & Time.
The post did not indicate whether the Dates are USA or International. A guess would be USA.
The logic is the same for USA or International systems but the Dates are formatted different mm-dd-yyyy versus dd-mm-yyyy
There are posts for the VBA and USA style dates.
One example is VBA to extract time only from Datetime format
I accidently posted my reply to that thread.

Date and Time 2021.xlsm
ABCDE
1DateTime
210-Jan-20215:37 PM10/01/21 17:3710-Jan-215:37 PM
3or5:37 PM
4
1d
Cell Formulas
RangeFormula
C2C2=A2+B2
D2D2=INT(C2)
E2E2=MOD(C2,1)
E3E3=C2-D2
 
Upvote 0
You're welcome, glad to help & thanks for the feedback. :)
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,164
Members
448,870
Latest member
max_pedreira

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