2016-12-01 91 views
0

你好家伙我完全难以理解为什么这段代码不工作。任何人都可以帮忙吗?Excel字符串不工作

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 
+0

你是什么意思“不工作”?该代码适用于我。 –

+0

也适用于我。 – Rdster

+0

我收到一个运行时错误13类型不匹配消息,并在选择调试时突出显示x = Day(RpDate)行。只有这个宏我有一个空白的新工作簿。 –

回答

1

你可能会迫使InputBox只允许Date类型有效值,试试下面的代码:

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