spliting one piece of informaiton into multiple variables (VBA)

smakatura

Board Regular
Joined
May 27, 2011
Messages
141
I want to ask a user for a date via an input box.
HTML:
date = InputBox("enter date of report")

the format of the date will remain consistant mm-dd-yy

and then separate out the parts into 3 variables. so
month = mm
day = dd
year = yy

what vba code would assign the month, day and year from the date input?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
Sub Macro1()
    Dim dDate As Date
    Dim y As Long, m As Long, d As Long
    
    dDate = Application.InputBox("Enter date.", Title:="Date Entry", Type:=1)
    If dDate = 0 Then Exit Sub 'User canceled
    
    y = Year(dDate)
    m = Month(dDate)
    d = Day(dDate)
    
    MsgBox "Year=" & y & vbCr & "Month=" & m & vbCr & "Day=" & d

End Sub
 
Upvote 0
something like this
Code:
Sub Foo()
'Date is a reserved word [can't be a variable as it's a function]
dtInput = Application.InputBox("enter date of report", Default:=Format(Date, "mm-dd-yy"), Type:=2)
dtMo = Left(dtInput, 2)
dtDa = Mid(dtInput, 4, 2)
dtYr = Right(dtInput, 2)
End Sub
 
Upvote 0
This should help...

Code:
Sub Foo()
MyDate = InputBox("Enter Date")
res = DateValue(MyDate)
yy = Year(res)
mm = Month(res)
dd = Day(res)
MsgBox mm & dd & yy
End Sub
 
Upvote 0
You don't want to use date as a variable name.

See the Year, Month, and Day functions.
 
Upvote 0
thanks. I actually had the variable rptdate instead of date. I just up the word date for the forum (now I know not to do that).

next part of the question. on the actual date function where it can be separated out by month ().... is there a particular 'symbol' that has to be between the numbers. ie does it have to be mm/dd/yy or can it also be mm-dd-yy?

It does not matter which one it is but I want to be make sure they write it correctly.
 
Upvote 0
This code will accept a variety of input formats. "5-23-2011", "5/23/11", "May 23, 2011" "23 May 2011" and some more
If the user types an unacceptable format, it will suggest an acceptable format.

Code:
Dim uiDateStr As String
Dim rptDate As Date
Dim strPrompt As String
Dim dd As Long, mm As Long, yy As Long

strPrompt = "Enter a Date"

d0
    uiDateStr = Application.InputBox(strPrompt, Type:=2)
    If uiDateStr = "False" Then Exit Sub: Rem canceled
    strPrompt = "Please enter a date in mm-dd-yyyy format"
Loop Until IsDate(uiDateStr)

rptDate = CDate(uiDateStr)
dd = Day(rptDate): mm = Month(rptDate): yy = Year(rptDate)
 
Upvote 0
I tried all the options
HTML:
dtInput = Application.InputBox("enter date of report", Default:=Format(Date, "mm-dd-yy"), Type:=2)
dtMo = Left(dtInput, 2)
dtDa = Mid(dtInput, 4, 2)
dtYr = Right(dtInput, 2)
Did not return the values




HTML:
Sub Macro1()
    Dim dDate As Date
    Dim y As Long, m As Long, d As Long
    
    dDate = Application.InputBox("Enter date.", Title:="Date Entry", Type:=1)
    If dDate = 0 Then Exit Sub 'User canceled
    
    y = Year(dDate)
    m = Month(dDate)
    d = Day(dDate)
    
    MsgBox "Year=" & y & vbCr & "Month=" & m & vbCr & "Day=" & d

End Sub
works but put the month of august (08) as 8. that does not work with opening files that say 08 not 8 in the name


HTML:
Sub Foo1()
rptdate = InputBox("Enter Date")
res = DateValue(MyDate)
yy = Year(res)
mm = Month(res)
dd = Day(res)
MsgBox mm & dd & yy
End Sub
Got "type mismatch on the res=... line



How would I get the mm, dd, yy variables to say 08 instead of 8?
 
Upvote 0
In all these codes mm,dd and yy are numbers. Leading zeros are a feature of strings, not numbers.
To display these numbers with leading zeros, one could use the Format function, as in
Code:
MsgBox Format(mm,"00")
 
Upvote 0
Code:
Sub Open_Dated_Filename()

    Dim dDate As Date
    Dim strFolder As String, strFile As String

    dDate = Application.InputBox("Enter date.", Title:="Date Entry", Type:=1)
    If dDate = 0 Then Exit Sub 'User canceled
    
    strFolder = "C:\Temp\"
    strFile = "MyFile" & Format(dDate, "mmddyyyy") & ".xls"  ' e.g. MyFile09292011.xls
    
    If Len(Dir(strFolder & strFile)) Then
        Workbooks.Open strFolder & strFile
    Else
        MsgBox "Cannot find file: " & strFile, vbExclamation, "File Not Found"
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

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