VBA相当于Excel的“搜索()”

时间:2015-09-16 20:28:59

标签: excel vba excel-vba

我有一项任务,我需要一些帮助。有三列:列A(用户名),列B(类别)和列C(注释)。我必须创建一个Sub,根据类别(例如Admin)检查B列中的每个单元格。如果在B2中找到字符串“Admin”,则搜索A2:“john.deer”,“BES”或“mars”。如果找到“john.deer”,则C2表示“域管理员帐户”,如果找到“BES”,则C2表示“BES管理员帐户”等。

但是,如果在C列中找不到“Admin”,则在C2中搜索“SQL”。如果找到“SQL”,则在A2中搜索“SQL Server”,“SQL Engine”和“SQL Cluster”。在B列和A列中搜索除了不同字符串之外的与前一段完全相同的任务。不同的字符串也输出到C列。

我有一个完美的方程式,只需要为VBA创建一个等价物:

=IF(NOT(ISERROR(SEARCH("admin",B3))),IF(NOT(ISERROR(SEARCH("john.deer",A3))),"Domain Admin Account",if(not(iserror(search("bes",a2))),"BES  Admin Account", if(not(iserror(search("mars",a2))),"Cisco Admin Account","no category"),IF(NOT(ISERROR(SEARCH("SQL",B3))),IF(NOT(ISERROR(SEARCH("sqlserver",A3))),"SQL Server",IF(NOT(ISERROR(SEARCH("sqlengine",B3))),"SQL Engine Account"," ")))

你可以告诉它一团糟,这就是为什么我希望创建一个等效的VBA。但是,VBA中没有Search()对象,只有.Find。

这是我在尝试搜索无效之前尝试VBA:

    Private Sub CommandButton21_Click()

If Not IsError Then
    If Search("ADMIN", "B2") Then 'Searches Col. F for "Admin", then Col. B for type of admin before
                                'populating Notes Col. with specific Note

    If Not IsError Then
        Search("john.deer", "A2") = "Domain Admin Account"
    ElseIf Not IsError Then
        Search("BES", "A2") = "BES Admin Account"
    ElseIf Not IsError Then
        Search("mars1", "A2") = "Admin Account for Cisco Phone System"
    Else
  End If

If Search("admin", "B2") Then
    If Not IsError Then
        Search("uccxadmin", "b2") = "Admin Account for Cisco phone system"

End If
end if
end sub

2 个答案:

答案 0 :(得分:1)

puts response == "a" ? "yes" : "no" 是VBA等效

Instr

Instr有比搜索更多的选项:请参阅文档here

答案 1 :(得分:0)

您可以在VBA代码中使用Excel中的任何可用功能。您可以在Application.WorksheetFunction对象中访问此函数:Application.WorksheetFunction.Search。使用您在电子表格中使用的相同参数。

相关问题