我有以下 VBA 代码,它为 InStr 函数返回“0”:
If InStr("ICD-10 & CPT OPC NHSN", "JCMC ICD-10 & CPT OPC NHSN Sep-17 new.xlsx") > 0 Then
'Do something
End If
但是,以下代码使用 WorksheetFunction.Find 函数将子字符串定位在位置 6:
If Application.WorksheetFunction.Find("ICD-10 & CPT OPC NHSN", "JCMC ICD-10 & CPT OPC NHSN Sep-17 new.xlsx") > 0 Then
'Do something
End If
为什么 InStr 函数未找到子字符串时 WorksheetFunction 返回 6?
最佳答案
如果你看the documentation for the InStr
function您会看到两个强制参数中的第一个是要搜索的字符串,第二个参数是要查找的字符串。
string1 Required. String expression being searched.
string2 Required. String expression sought.
所以你的代码需要
If InStr("JCMC ICD-10 & CPT OPC NHSN Sep-17 new.xlsx", "ICD-10 & CPT OPC NHSN") > 0 Then
'Do something
End If
关于excel - Instr 函数未找到子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47640083/