簡體   English   中英

使用VBA或Excel公式在Excel單元格中搜索部分字符串

[英]Search for a partial string in the excel cell using VBA or Excel formula

我有一個帶有列模板和模板名稱的Excel工作表,我必須在模板列中用部分字符串填充模板名稱列:

    Template                                 Template Name
    This is String  
    String line contains data
    This is Int 
    Int line contain data    
    int: in the String data
    string: in the int data 
    Int: is empty or no string 

我嘗試了以下公式,但給出了不同的結果:

     =IF(ISNUMBER(SEARCH("string",A2)),"string",IF(ISNUMBER(SEARCH("int",A2)),"int",IF(ISNUMBER(SEARCH("int:",A2)),"int",IF(ISNUMBER(SEARCH("string:",A2)),"string"))))

B列中的公式給出的輸出:

    Template                         Template Name
    This is String                       String
    String line contains data            string
    This is Int                          int
    Int line contain data                int 
    int: in the String data              string    #it must be 'int'
    string: in the int data              int       #it must be 'string'
    Int: is empty or no string           string    #it must be 'int'

請讓我知道必須更改什么才能獲得所需的輸出。

重新排列搜索順序:如果首先搜索較長的字符串(帶有':'),然后搜索沒有':'的字符串,則結果應該正確。

=IF(ISNUMBER(SEARCH("string:",A2)),"string",IF(ISNUMBER(SEARCH("int:",A2)),"int",IF(ISNUMBER(SEARCH("int",A2)),"int",IF(ISNUMBER(SEARCH("string",A2)),"string"))))

編輯 :使代碼適合直接在電子表格單元格中輸入(我以前認為是VBA宏)

如果要在“字符串”和“整數”之間第一次出現,請使用

=IF(IFERROR(SEARCH("int",RC1),10000)>IFERROR(SEARCH("string",RC1),10000),"String", "Int")"

您應該處理字符串中既不是“字符串”也不是“ int”的情況,那么只需檢查其中一個是否出現

暫無
暫無

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

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