简体   繁体   English

VBA等同于Excel的“ Search()”

[英]VBA equivalent to Excel's “Search()”

I have an assignment I need some help with. 我有一项作业需要帮助。 There are three columns: Column A(username), Column B(Category), and Column C(note). 一共有三列:A列(用户名),B列(类别)和C列(注释)。 I have to create a Sub that checks each cell down Column B according to category (for example Admin). 我必须创建一个Sub,根据类别(例如Admin)检查B列下方的每个单元格。 If the string, "Admin", is found in B2 for example, then the A2 is searched for either:"john.deer", "BES", or "mars". 例如,如果在B2中找到字符串“ Admin”,则在A2中搜索:“ john.deer”,“ BES”或“ mars”。 If "john.deer" is found then C2 is to say "Domain Admin Account", if "BES" is found then C2 is to say "BES Admin Account" etc. 如果找到“ john.deer”,则C2表示“域管理员帐户”,如果找到“ BES”,则C2表示“ BES管理员帐户”等。

However if "Admin" is not found in Column C then "SQL" is searched in C2. 但是,如果在列C中找不到“ Admin”,则在C2中搜索“ SQL”。 If "SQL" is found then "SQL Server", "SQL Engine", and "SQL Cluster" is searched in A2. 如果找到“ SQL”,则在A2中搜索“ SQL Server”,“ SQL Engine”和“ SQL Cluster”。 The exact same task as the previous paragraph except different strings are searched in Column B and A. Different strings are outputted to Column C as well. 与上一段完全相同的任务,只是在B列和A列中搜索不同的字符串。不同的字符串也输出到C列。

I had an equation that was perfect and just needed to create an equivalent for VBA: 我有一个完美的方程,只需要为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"," ")))

As you can tell its a mess, which is why I wish to create a VBA equivalent. 如您所知,这是一团糟,这就是为什么我希望创建一个等效的VBA。 However, there is no Search() object in VBA, only .Find. 但是,VBA中没有Search()对象,只有.Find。

Here is my attempt at VBA before realizing Search did not work: 这是我在实现Search无效之前尝试使用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

Instr is the VBA eqivalent Instr与VBA等价

dim Pos as long
Pos=Instr(Range("B2"),"ADMIN")
if pos>0 then 'code if found
else 'notfound
end if

Instr has more options than search: see documentation here . Instr除了搜索外还有更多选择:请参阅此处的文档。

You can use any function available in Excel in your VBA code. 您可以在VBA代码中使用Excel中可用的任何功能。 You can access this function in Application.WorksheetFunction object: Application.WorksheetFunction.Search . 您可以在Application.WorksheetFunction对象Application.WorksheetFunction.Search访问此函数。 Use the same arguments you would use in the spreadsheet. 使用与电子表格中相同的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM