使用 Match 或任何搜索函数获取类型不匹配
Getting a type mismatch using Match or any Search Function
我这两天一直在努力让它工作。我试图在我的工作表中找到一个特定的单词字符串,其中我有相似的单词字符串(例如:Yellow-Banana-F
和 Yellow-Banana-WF
)。我曾多次尝试以不同的方式更改我的代码(例如 String
与 Long
对于 Dim ILS1/2
),并且只能成功找到包含其中一个值的第一行,即使我我试图找到所有适用的匹配项并将它们排序到我的用户表单中(例如:如果 Yellow-Banana-F
是 ̀ Yellow-Banana-WF, then my code will only find and display
Yellow-Banana-F` 上方的一行在正确的位置,但是另一个根本不会显示)。
当前代码抛出错误
"Run time error 13: type mismatch."
我也尝试过使用循环,但我似乎也无法让它工作。我是通过 VBA 自学的,没有广泛的知识,所以也许我在这里遗漏了一些非常简单的东西,但无论哪种方式,我都很高兴听到任何建议!
当前代码:
Private Sub CHK1_change()
Dim sh6 as Worksheet
Set sh6 = ThisWorkbook.Sheets("OOS")
Dim ILS1 As String
Dim ILS2 As String
ILSA1 = Me.txtILS.value & "-WF"
ILSA2 = Me.txtILS.value & "-F"
ILS1 = Application.Match(VBA.CStr(ILSA1), sh6.Range("B:B"), 0)
ILS2 = Application.Match(VBA.CStr(ILSA2), sh6.Range("B:B"), 0)
If sh6.range("F" & ILS1). value <> "" then
me.txtILSA1.value = "WF"
If sh6.range("F" & ILS2).value <> "" then
me.txtILSA2.value = "F"
End Sub
使用Application.Match
- 最好将结果写入变体变量 (
sIndex
)。
- 结果 (
sIndex
) 将是引用第一个找到值的索引(位置)的整数,如果不匹配则为错误值。
- 您可以通过以下两种方式测试结果(
sIndex
):
If IsNumeric(sIndex) Then ' used in the code
If IsError(sIndex) Then
快速修复
Option Explicit
Private Sub CHK1_change()
Dim sh6 As Worksheet: Set sh6 = ThisWorkbook.Worksheets("OOS")
Dim Suffixes As Variant: Suffixes = Array("WF", "F")
Dim TextBoxes As Variant: TextBoxes = Array(Me.txtILSA1, Me.txtILSA2)
Dim ILSA As String
Dim sIndex As Variant
Dim n As Long
For n = LBound(Suffixes) To UBound(Suffixes)
ILSA = Me.txtILS.Value & "-" & Suffixes(n)
sIndex = Application.Match(ILSA, sh6.Range("B:B"), 0)
If IsNumeric(sIndex) Then ' is number
If sh6.Range("F" & sIndex).Value <> "" Then ' cell not blank
TextBoxes(n).Value = Suffixes(n)
'Else ' cell (in column 'F') is blank
End If
'Else ' is error value
End If
Next n
End Sub
我这两天一直在努力让它工作。我试图在我的工作表中找到一个特定的单词字符串,其中我有相似的单词字符串(例如:Yellow-Banana-F
和 Yellow-Banana-WF
)。我曾多次尝试以不同的方式更改我的代码(例如 String
与 Long
对于 Dim ILS1/2
),并且只能成功找到包含其中一个值的第一行,即使我我试图找到所有适用的匹配项并将它们排序到我的用户表单中(例如:如果 Yellow-Banana-F
是 ̀ Yellow-Banana-WF, then my code will only find and display
Yellow-Banana-F` 上方的一行在正确的位置,但是另一个根本不会显示)。
当前代码抛出错误
"Run time error 13: type mismatch."
我也尝试过使用循环,但我似乎也无法让它工作。我是通过 VBA 自学的,没有广泛的知识,所以也许我在这里遗漏了一些非常简单的东西,但无论哪种方式,我都很高兴听到任何建议!
当前代码:
Private Sub CHK1_change()
Dim sh6 as Worksheet
Set sh6 = ThisWorkbook.Sheets("OOS")
Dim ILS1 As String
Dim ILS2 As String
ILSA1 = Me.txtILS.value & "-WF"
ILSA2 = Me.txtILS.value & "-F"
ILS1 = Application.Match(VBA.CStr(ILSA1), sh6.Range("B:B"), 0)
ILS2 = Application.Match(VBA.CStr(ILSA2), sh6.Range("B:B"), 0)
If sh6.range("F" & ILS1). value <> "" then
me.txtILSA1.value = "WF"
If sh6.range("F" & ILS2).value <> "" then
me.txtILSA2.value = "F"
End Sub
使用Application.Match
- 最好将结果写入变体变量 (
sIndex
)。 - 结果 (
sIndex
) 将是引用第一个找到值的索引(位置)的整数,如果不匹配则为错误值。 - 您可以通过以下两种方式测试结果(
sIndex
):
If IsNumeric(sIndex) Then ' used in the code
If IsError(sIndex) Then
快速修复
Option Explicit
Private Sub CHK1_change()
Dim sh6 As Worksheet: Set sh6 = ThisWorkbook.Worksheets("OOS")
Dim Suffixes As Variant: Suffixes = Array("WF", "F")
Dim TextBoxes As Variant: TextBoxes = Array(Me.txtILSA1, Me.txtILSA2)
Dim ILSA As String
Dim sIndex As Variant
Dim n As Long
For n = LBound(Suffixes) To UBound(Suffixes)
ILSA = Me.txtILS.Value & "-" & Suffixes(n)
sIndex = Application.Match(ILSA, sh6.Range("B:B"), 0)
If IsNumeric(sIndex) Then ' is number
If sh6.Range("F" & sIndex).Value <> "" Then ' cell not blank
TextBoxes(n).Value = Suffixes(n)
'Else ' cell (in column 'F') is blank
End If
'Else ' is error value
End If
Next n
End Sub