2

I am relatively new to VBA, and I need some help on a code I have been writing. Currently, it looks like this:

Sub RoundedRectangle1_Click()
Selection.NumberFormat = "dd mmm yy"
Range("H2").ClearContents
 Dim Date1 As ValueChange
Range("H2").Value = InputBox("Enter the first date (Monday) of the week you wish to view, in the format DD/MM")
End Sub

As you can see, I have a pop-up box for the user to manually enter the date, but for some reason, once this is entered it keeps providing an answer in the US format, for instance if I type in 04/12, this will appear as "12 Apr 16", rather than "04 Dec 16"

CBooth
  • 41
  • 3

2 Answers2

0

According to my tests, InputBox returns a string. What I would do, is write the following function (just demo, no error handling in this code):

Private Function ParseDate(sInput As String) As Date
    Dim sTmp() As String
    sTmp = Split(sInput, "/")

    ParseDate = DateTime.DateSerial(2016, sTmp(1), sTmp(0))
End Function

and then simply call it like this:

 Dim sResult As String
 sResult = InputBox("Enter the first date (Monday) of the week you wish to view, in the format DD/MM")
 Range("H2").Value = ParseDate(sResult)
Robert J.
  • 2,631
  • 8
  • 32
  • 59
0

This macro, which asks for the date to be printed at the head of an attendance register works for dd/mm/yy or dd/mm in the 21st century. Could easily be adapted to include 20th cent

Sub Print_Register()
'
' Print_Register Macro
Dim MeetingDate, Answer

    Sheets("Register").Select
    Range("A1").Select
GetDate:
    MeetingDate = DateValue(InputBox("Enter the date of the meeting." & Chr(13) & _
    "Note Format" & Chr(13) & "Format DD/MM/YY or DD/MM", "Meeting Date", , 10000, 10000))
    If MeetingDate = "" Then GoTo TheEnd
    If MeetingDate < 36526 Then MeetingDate = MeetingDate + 36525 'If no yy add year 2000
    Range("Current_Meeting_Date") = MeetingDate
    Answer = MsgBox("Date OK?", 3)
    If Answer = 2 Then GoTo TheEnd
    If Answer = 7 Then GoTo GetDate
    ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,,,TRUE,,FALSE)"
TheEnd:
End Sub
bummi
  • 27,123
  • 14
  • 62
  • 101
Ken Ince
  • 11
  • 1