嗨,我有一个代码,我没有做任何更改,但我得到 Type mismatch error
.它以前工作正常。
代码 :
Private Sub UserForm_Initialize()
Dim r As Integer
Dim c As Integer
Me.ComboBox1.RowSource = "Intro!A3:A" & Range("A" & Rows.Count).End(xlUp).Row
r = Application.Match("c", Columns(1), 0)
c = Application.Match("cc", Rows(1), 0)
TextBox1.Value = Cells(r, c).Value
TextBox2.Value = Cells(r, c + 1).Value
TextBox3.Value = Cells(r, c + 2).Value
TextBox4.Value = Cells(r, c + 3).Value
End Sub
使用
f8
运行到达此处时通过代码发现错误: c = Application.Match("cc", Rows(1), 0)
此行的目标是在第一行中找到匹配项
最佳答案
使用 Match
时您应该始终为 Match
的场景准备代码未能找到您要查找的值(或字符串)。
您可以使用以下方法做到这一点:
If Not IsError(Application.Match("cc", Columns(1), 0)) Then
修改代码
Dim r As Variant
Dim c As Variant
Me.ComboBox1.RowSource = "Intro!A3:A" & Range("A" & Rows.Count).End(xlUp).Row
If Not IsError(Application.Match("c", Columns(1), 0)) Then
r = Application.Match("c", Columns(1), 0)
Else
' in case "c" is not found
MsgBox "critical error finding 'c'"
End If
If Not IsError(Application.Match("cc", Columns(1), 0)) Then
c = Application.Match("cc", Columns(1), 0)
Else
' in case "cc" is not found
MsgBox "critical error finding 'cc'"
End If
关于vba - 无需对 VBA 代码进行任何修改即可获取类型不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49377341/