types不匹配错误instr

我想在保单持有人(NRIC)中findIC号码。 但是在代码中有错误。

Dim col As Long Dim IC, NRIC Dim name name = ActiveSheet.Cells(rw, 1).Value NRIC = ThisWorkbook.Sheets("main").Columns(9) IC = InStr(1, name, "(" & NRIC & ")") <<<<< this is the problem (type mismatch error) MsgBox IC 

显示器:

主表

名称列

  • 雪莉酒

IC专栏

  • S1233456c

政策细节表

政策所有者(NRIC)专栏

  • 雪利酒(S1233456c)

InStr函数第三个参数需要一个string( 子string )。
你实际上传递了一个数组,它是整个列(9)。
为了工作,你需要遍历第(9)列中的所有值,如下所示:

 Dim c As Range With ThisWorkbook.Sheets("main") For Each c In .Columns(9).CurrentRegion If InStr(1, name, c.Value2) <> 0 Then MsgBox c.Value2 ' I don't know what you want to do if you find the cell Exit Sub End If Next End With 

另一种Find Method是使用范围对象的Find Method

 Dim c As Range With ThisWorkbook.Sheets("main") Set c = .Columns(9).CurrentRegion.Find(What:=name, LookAt:=xlPart) If Not c Is Nothing Then MsgBox c.Value2 End With