0

Hello guys I am completely stumped as to why this bit of code is not working. Can anybody help?

Sub test()

Dim RpDate As Variant
Dim x As String

RpDate = InputBox("Enter Date", "Date")
If RpDate = "" Then Exit Sub

x = Day(RpDate)

MsgBox x

End Sub
9
  • What do you mean "Not Working"? The code works for me. Commented Dec 1, 2016 at 18:19
  • Works for me too. Commented Dec 1, 2016 at 18:22
  • I get a run time error 13 type mismatch message with the x = Day(RpDate) line highlighted when I select debug. I have a blank new workbook with just this macro. Commented Dec 1, 2016 at 18:25
  • @Rdster I tried your suggestion and I still get the same error. I can't see why this wouldn't work for me but it isn't. Commented Dec 1, 2016 at 18:26
  • 3
    What are you trying to enter as a date? Commented Dec 1, 2016 at 18:31

1 Answer 1

1

You could force the InputBox to allow only Date type valid values, try the code below:

Option Explicit

Sub InputBoxDateFormat()

Dim RpDate As Date
Dim x As Integer

' InputBox that allows only dates
RpDate = Application.InputBox("Enter Date", "Date", FormatDateTime(Date, vbShortDate), Type:=1)

' "Cancel" was selected
If RpDate = 0 Then Exit Sub

x = Day(RpDate)
MsgBox x

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.