VBA Code to extract date range from Cell?

Coyotex3

Well-known Member
Joined
Dec 12, 2021
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Hi, trying to figure out a code which will allow me to extract a date range from a cell. I was able to do it using Formulas(TextBefore and TextAfter) but would like to be able to insert this inside of a larger code.

Ideally, I'd like to go from this:

Book2
ABCD
1Account TypeNotesTotalDate Range
2Bills1357888 (1/01/2020-2/01/2020)1000
3BillsMonthly (1/01/2020-2/01/2020)500
4BillsExpenses (02/01/2021-3/01/2021)1000
5Bills6000 (1/01/2020-2/01/2020)500
Sheet1


To this:

Book2
ABCD
1Account TypeNotesTotalDate Range
2Bills1357888 (1/01/2020-2/01/2020)10001/01/2020-2/01/2020
3BillsMonthly (1/01/2020-2/01/2020)5001/01/2020-2/01/2020
4BillsExpenses (02/01/2021-3/01/2021)100002/01/2021-3/01/2021
5Bills6000 (1/01/2020-2/01/2020)5001/01/2020-2/01/2020
Sheet1


Note: Range D3 contains a different date format than the other cells in Column D. The date ranges will sometimes be written as 01/01/2020 or 1/1/2020 or 01/1/2020 or 1/01/2020 etc. Is there a way of making excel recognize the different date formats and extracting them?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,
You can test following formula :
Excel Formula:
=MID(B2,FIND("(",B2)+1,LEN(B2)-FIND("(",B2)-1)
 
Upvote 0
Hi,
You can test following formula :
Excel Formula:
=MID(B2,FIND("(",B2)+1,LEN(B2)-FIND("(",B2)-1)
Hi James, thank you for this formula. It worked along with the one below. Ideally I'd like to be able to do it with VBA as this will be placed inside of a larger Sub.
VBA Code:
=TEXTAFTER(TEXTBEFORE(B2,")"),"(")
 
Upvote 0
Try this and see if it works for you.
VBA Code:
Sub getDateRange()
Dim wb As Workbook, sht As Worksheet, dRng As Range, fRng As Range, cell As Range
Dim i As Long, x As Long, y As Long
Dim d1 As String, d2 As String, dFull As String
Set wb = ThisWorkbook: Set sht = wb.Worksheets("Sheet1")
Set dRng = sht.Range(Cells(2, 2), Cells(sht.UsedRange.Rows.Count, 2))
Set fRng = sht.Range(Cells(2, 4), Cells(sht.UsedRange.Rows.Count, 4))
For Each cell In dRng
    dFull = Right(cell.Value, Len(cell.Value) - InStr(1, cell.Value, "("))
    dFull = Left(dFull, Len(dFull) - 1)
    d1 = Left(dFull, InStr(1, dFull, "-") - 1)
    d2 = Right(dFull, Len(dFull) - InStr(1, dFull, "-"))
    cell.Offset(0, 2).Value = d1 & "-" & d2
Next cell
End Sub
 
Upvote 0
Try this and see if it works for you.
VBA Code:
Sub getDateRange()
Dim wb As Workbook, sht As Worksheet, dRng As Range, fRng As Range, cell As Range
Dim i As Long, x As Long, y As Long
Dim d1 As String, d2 As String, dFull As String
Set wb = ThisWorkbook: Set sht = wb.Worksheets("Sheet1")
Set dRng = sht.Range(Cells(2, 2), Cells(sht.UsedRange.Rows.Count, 2))
Set fRng = sht.Range(Cells(2, 4), Cells(sht.UsedRange.Rows.Count, 4))
For Each cell In dRng
    dFull = Right(cell.Value, Len(cell.Value) - InStr(1, cell.Value, "("))
    dFull = Left(dFull, Len(dFull) - 1)
    d1 = Left(dFull, InStr(1, dFull, "-") - 1)
    d2 = Right(dFull, Len(dFull) - InStr(1, dFull, "-"))
    cell.Offset(0, 2).Value = d1 & "-" & d2
Next cell
End Sub
Hi, Thank you for your reply.

I'm getting Run time Error 1004 on this line
VBA Code:
Set dRng = sht.Range(Cells(2, 2), Cells(sht.UsedRange.Rows.Count, 2))
 
Upvote 0
Hi,
With your formula transposed in a macro, you can test
VBA Code:
For i = 2 To 20
    Range("D" & i) = Evaluate("=TEXTAFTER(TEXTBEFORE(B" & i & ","")""),""("")")
Next i
 
Upvote 1
Solution
You can probably add standardising the date format to @James006's evaluate function but here is another option.
Note: It assumes that the date is in the format of your region setting. If you need to reverse day & month it will need to be modified.

VBA Code:
Sub SplitOutAndFormatDate()

    Dim wb As Workbook
    Dim sht As Worksheet
    Dim Rng As Range, destRng As Range
    Dim arr As Variant
    Dim dtFrom As Date, dtTo As Date
    Dim DateFormat As String
    Dim strSplit As Variant
    Dim LastRow As Long, i As Long
    
    Set wb = ActiveWorkbook
    Set sht = wb.Worksheets("Sheet1")       '<--- Change this to the name of your sheet or to ActiveSheet
    DateFormat = "dd/mm/yyyy"               '<--- Change to desired format
    
    LastRow = sht.Range("B" & Rows.Count).End(xlUp).Row
    With sht
        Set Rng = .Range("B2:D" & LastRow)
        arr = Rng.Value
    End With

    For i = 1 To UBound(arr)
        strSplit = Split(arr(i, 1), "(")
        strSplit = Split(Left(strSplit(1), Len(strSplit(1)) - 1), "-")
        dtFrom = DateValue(strSplit(1))
        dtTo = DateValue(strSplit(1))
        arr(i, 3) = Format(dtFrom, DateFormat) & "-" & Format(dtTo, DateFormat)
    Next i
    
    Rng.Columns(3) = Application.Index(arr, 0, 3)

End Sub
 
Upvote 1
You can probably add standardising the date format to @James006's evaluate function but here is another option.
Note: It assumes that the date is in the format of your region setting. If you need to reverse day & month it will need to be modified.

VBA Code:
Sub SplitOutAndFormatDate()

    Dim wb As Workbook
    Dim sht As Worksheet
    Dim Rng As Range, destRng As Range
    Dim arr As Variant
    Dim dtFrom As Date, dtTo As Date
    Dim DateFormat As String
    Dim strSplit As Variant
    Dim LastRow As Long, i As Long
   
    Set wb = ActiveWorkbook
    Set sht = wb.Worksheets("Sheet1")       '<--- Change this to the name of your sheet or to ActiveSheet
    DateFormat = "dd/mm/yyyy"               '<--- Change to desired format
   
    LastRow = sht.Range("B" & Rows.Count).End(xlUp).Row
    With sht
        Set Rng = .Range("B2:D" & LastRow)
        arr = Rng.Value
    End With

    For i = 1 To UBound(arr)
        strSplit = Split(arr(i, 1), "(")
        strSplit = Split(Left(strSplit(1), Len(strSplit(1)) - 1), "-")
        dtFrom = DateValue(strSplit(1))
        dtTo = DateValue(strSplit(1))
        arr(i, 3) = Format(dtFrom, DateFormat) & "-" & Format(dtTo, DateFormat)
    Next i
   
    Rng.Columns(3) = Application.Index(arr, 0, 3)

End Sub
Alex, thank you!

This one works as well!
 
Upvote 0
Glad to hear you have managed to solve your problem (y)
 
Upvote 0

Forum statistics

Threads
1,223,877
Messages
6,175,139
Members
452,615
Latest member
bogeys2birdies

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