簡體   English   中英

VBA等同於Excel的“ Search()”

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

我有一項作業需要幫助。 一共有三列: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。

這是我在實現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與VBA等價

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

Instr除了搜索外還有更多選擇:請參閱此處的文檔。

您可以在VBA代碼中使用Excel中可用的任何功能。 您可以在Application.WorksheetFunction對象Application.WorksheetFunction.Search訪問此函數。 使用與電子表格中相同的參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM