Excel Application.Match运行时错误'13'

时间:2018-08-07 09:13:11

标签: vba excel-vba

我正在尝试使用application.match来允许我在表列中查找匹配日期的位置,但我无法使其正常工作,在1004错误和不匹配错误之间,没有Google结果似乎有帮助。

下面是代码,在我尝试使用或显示匹配结果之前,它运行良好

Private Sub ComboBox1_Click()
Me.ComboBox1.Value = Format(Me.ComboBox1.Value, "dd/mm/yy")
TheDate = ComboBox1.Value
End Sub

Private Sub Button1_Click()
Dim TheDate As Variant
TheDate = ComboBox1.Value
Pos = Application.Match(TheDate, Sheet1.Range("B5:B30"), 0)
Label1.Caption = Pos
End Sub

我是新来的,所以如果它很明显,我不会感到惊讶

谢谢

2 个答案:

答案 0 :(得分:0)

Excel将日期存储为数字...试试吧

Pos = Application.Match(CLng(CDate(TheDate)), Sheet1.Range("B5:B30"), 0)

屏幕截图

enter image description here

答案 1 :(得分:0)

可能是

Dim theDate As Date

Private Sub CommandButton1_Click()
Dim pos As Variant

pos = Application.match(CLng(theDate), Sheet1.Range("B5:B30"), 0)
If Not IsError(pos) Then
    Label1.Caption = pos
Else
    MsgBox "No Such Date", vbExclamation
End If
End Sub

Private Sub UserForm_Initialize()
theDate = Date
Me.ComboBox1.Value = Format(Date, "dd/mm/yy")
End Sub