Pulling date from string

vzq032372

New Member
Joined
Dec 17, 2015
Messages
37
Hello All,

I'm not very familiar with creating my own functions but found this online and am trying to manipulate it to my own need. I recorded the formulas and then replaced the range address with "strInputDate" but I'm still getting an "invalid procedure call or argument". it stops at the first formula for month. Any clues?

VBA Code:
Public Function getDate(strInputDate As String) As Date
    'Variable Declaration
    Dim iMonth As Integer
    Dim iDay As Integer
    Dim lYear As Long
    '
    'Get month
        iMonth = Mid(strInputDate, InStr(":", strInputDate) + 1, InStr("/", strInputDate) - 1 - InStr(":", strInputDate))
'
    'Get day
        iDay = Mid(strInputDate, InStr("/", strInputDate) + 1, InStr("/", strInputDate, InStr("/", strInputDate) + 1) - InStr("/", strInputDate) - 1)
        
    'Get year
     If Len(Mid(strInputDate, InStr("/", strInputDate, InStr("/", strInputDate) + 1) + 1, 4)) = 2 Then
        lYear = "20" & Mid(strInputDate, InStr("/", strInputDate, InStr("/", strInputDate) + 1) + 1, 4)
        
        Else: iYear = Mid(strInputDate, InStr("/", strInputDate, InStr("/", strInputDate) + 1) + 1, 4)
    End If
    'Create a complete date and return the value
    getDate = CDate(MonthName(iMonth) & "/" & iDay & "/" & lYear)
    '
End Function
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I think it would be better if you show us a bunch of examples of what your strings look like, and what you are trying to pull from them.
 
Upvote 0
Attached is a sample image, I couldn't figure out how to post the file. Basically I need to extract the dates from Q8 and Q10. These dates will not always be in these fields and they will be in different formats mm/d/yyyy, mm/d/yy or m/d/yy or mm/dd/yyyy (this is the format I ultimately want)
 

Attachments

  • Screenshot 2022-09-12 104008.png
    Screenshot 2022-09-12 104008.png
    2.2 KB · Views: 5
Upvote 0
Hello All,

I'm not very familiar with creating my own functions but found this online and am trying to manipulate it to my own need. I recorded the formulas and then replaced the range address with "strInputDate" but I'm still getting an "invalid procedure call or argument". it stops at the first formula for month. Any clues?

VBA Code:
Public Function getDate(strInputDate As String) As Date
    'Variable Declaration
    Dim iMonth As Integer
    Dim iDay As Integer
    Dim lYear As Long
    '
    'Get month
        iMonth = Mid(strInputDate, InStr(":", strInputDate) + 1, InStr("/", strInputDate) - 1 - InStr(":", strInputDate))
'
    'Get day
        iDay = Mid(strInputDate, InStr("/", strInputDate) + 1, InStr("/", strInputDate, InStr("/", strInputDate) + 1) - InStr("/", strInputDate) - 1)
      
    'Get year
     If Len(Mid(strInputDate, InStr("/", strInputDate, InStr("/", strInputDate) + 1) + 1, 4)) = 2 Then
        lYear = "20" & Mid(strInputDate, InStr("/", strInputDate, InStr("/", strInputDate) + 1) + 1, 4)
      
        Else: iYear = Mid(strInputDate, InStr("/", strInputDate, InStr("/", strInputDate) + 1) + 1, 4)
    End If
    'Create a complete date and return the value
    getDate = CDate(MonthName(iMonth) & "/" & iDay & "/" & lYear)
    '
End Function

I think it would be better if you show us a bunch of examples of what your strings look like, and what you are trying to pull from them.
Samplebook.xlsx
BCDEFGHIJKLMNOPQRSTUVWXYZ
1
2
3E S T I M A T E S U M M A R Y
4
5
6Project Title: Line 125Estimate By:
7
8Project Mgr/Lead: 2020Date of Estimate: 02-28-20
912
10Project Number:2016915ISD: 12/15/20
11WO&Task#: 02368367
12Est. Revision # 00Estimate # E-20-51
13Template Revision # 01.3
14
15ESTIMATE SUMMARY
16
17Estimate Type: Planning
18
19WBSTOTAL% of TotalPrior202020212022202320242025
201Construction$10,397,20054.4%$10,397,200
212Engineering / Design$633,5003.3%$6,500$627,000
223Land
234Material$2,126,70011.1%$2,126,700
245Project Mgmt. & Sppt.$925,4004.8%$1,400$924,000
256Removal$423,4002.2%$423,400
267Test
278Risk / Contingency$975,0005.1%$975,000
289Escalation
2910Indirects$3,015,30015.8%$3,200$3,012,100
3011AFUDC$603,6003.2%$603,600
31 Total Cost$19,100,100100.0%$11,100$19,089,000
32
33
34Estimate Range-25%-25%
35$14,325,000-$23,875,000
36
37NOTE: Risk / Contingency is 5.4% of Project Costs (incl escal/indirects/AFUDC)
38
39COMMENTS:
40
41Project Scope:
42
43
44
45
46
47
48
Cover
Cell Formulas
RangeFormula
M8M8=IF(LEN(MID(Q10, FIND("/",Q10, FIND("/",Q10)+1)+1,4))=2,"20" & MID(Q10, FIND("/",Q10, FIND("/",Q10)+1)+1,4),MID(Q10, FIND("/",Q10, FIND("/",Q10)+1)+1,4))
M9M9=MID(Q10,FIND(":",Q10)+1,FIND("/",Q10)-1-FIND(":",Q10))
M10M10=MID(Q10,FIND("/",Q10)+1,FIND("/",Q10,FIND("/",Q10)+1)-FIND("/",Q10)-1)
 
Upvote 0
Could tru a amll UDF:
VBA Code:
Function FixDate(str As String) As Date
    Dim re As Object
    
    Set re = CreateObject("vbscript.regexp")
    With re
        .Pattern = "[^\d+/-]"
        .Global = True
        FixDate = CDate(.Replace(str, ""))
    End With
End Function

Used like:
Book1
BC
2Date of Estimate: 02-28-2028/02/2020
3ISD@ 12/13/2013/12/2020
4Cheese 4/18/202218/04/2022
5Sandwich 03/21/2221/03/2022
Sheet1
Cell Formulas
RangeFormula
C2:C5C2=FixDate(B2)
 
Upvote 0
Solution
Could tru a amll UDF:
VBA Code:
Function FixDate(str As String) As Date
    Dim re As Object
   
    Set re = CreateObject("vbscript.regexp")
    With re
        .Pattern = "[^\d+/-]"
        .Global = True
        FixDate = CDate(.Replace(str, ""))
    End With
End Function

Used like:
Book1
BC
2Date of Estimate: 02-28-2028/02/2020
3ISD@ 12/13/2013/12/2020
4Cheese 4/18/202218/04/2022
5Sandwich 03/21/2221/03/2022
Sheet1
Cell Formulas
RangeFormula
C2:C5C2=FixDate(B2)
Although there is a date it's returning the following (see image). The dates will always have the month first, that might be the error.
1662996070310.png
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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