Excel 使用vba进行查找
我的工作簿中有一个值(变量)Excel 使用vba进行查找,excel,vba,Excel,Vba,我的工作簿中有一个值(变量)strCompany。我想确定另一个工作簿tmp_工作簿的A列中是否存在此值。如果不存在,则应该有一个消息框。下面的代码有意义吗(我在前面的代码中定义了变量tmp_工作簿和strCompany)?如果没有,也许你可以建议一个更好的方法 On Error GoTo ErrorHandler Set value_exists_in_table = tmp_workbook.ActiveSheet.Range("A1:A100000").Find(strCompa
strCompany
。我想确定另一个工作簿tmp_工作簿的A列中是否存在此值。如果不存在,则应该有一个消息框。下面的代码有意义吗(我在前面的代码中定义了变量tmp_工作簿和strCompany)?如果没有,也许你可以建议一个更好的方法
On Error GoTo ErrorHandler
Set value_exists_in_table = tmp_workbook.ActiveSheet.Range("A1:A100000").Find(strCompany)
ErrorHandler:
Select Case Err.Number
Case 9, 91
MsgBox "The company " & strCompany & " was not found."
Exit Sub
End Select
继承上次使用时的许多参数;通常由用户在工作表上填写。您应该明确指定几个更常用的参数,如LookAt:xlother
或LookAt:xlPart
和LookIn:=xlValues
或LookIn:=xlFormulas
我通常远离。查找一行或一列中的精确匹配项。对象的本机在查找值方面做得很好
dim rw as variant
with worksheets("Sheet1")
rw = application.match(strCompany, .Columns(1), 0)
if not iserror(rw) then
value_exists_in_table = .cells(rw, 1).value
debug.print value_exists_in_table & " found in row " & rw
else
debug.print "The company " & strCompany & " was not found."
end if
end with
继承上次使用时的许多参数;通常由用户在工作表上填写。您应该明确指定几个更常用的参数,如LookAt:xlother
或LookAt:xlPart
和LookIn:=xlValues
或LookIn:=xlFormulas
我通常远离。查找一行或一列中的精确匹配项。对象的本机在查找值方面做得很好
dim rw as variant
with worksheets("Sheet1")
rw = application.match(strCompany, .Columns(1), 0)
if not iserror(rw) then
value_exists_in_table = .cells(rw, 1).value
debug.print value_exists_in_table & " found in row " & rw
else
debug.print "The company " & strCompany & " was not found."
end if
end with
阿琼,你能详细谈谈你的问题吗?你哪里有困难?我建议也使用文档。Range.Find方法的文档,该方法在未找到匹配项时查找retuns
Nothing
。否则,它会返回“找到该信息的第一个单元格”Arjun,您能详细说明您的问题吗?你哪里有困难?我建议也使用文档。Range.Find方法的文档,该方法在未找到匹配项时查找retunsNothing
。否则,它将返回“找到该信息的第一个单元格”