简体   繁体   English

比较两列Excel VBA中的唯一字符串

[英]Comparing unique strings in two columns Excel VBA

I am trying to find a string in Column D and Column H and if they match certain criteria, insert the string "Match" in Column W. 我试图在D列和H列中找到一个字符串,如果它们符合某些条件,请在W列中插入字符串“Match”。

For example, if the string "Auditorium" occurs in Column D and the string "INTERNAL" occurs in Column H on the same row, insert the string "Match" in Column W. 例如,如果字符串“Auditorium”出现在D列中,字符串“INTERNAL”出现在同一行的H列中,则在W列中插入字符串“Match”。

My code below inserts the string "Match" in Column W against all rows that contain the string "INTERNAL" irrespective of the string in Column D! 我的代码在列W中插入字符串“Match”,对着包含字符串“INTERNAL”的所有行,而不管D列中的字符串!

Any advice would be appreciated! 任何意见,将不胜感激!

Row | 排| Column D | D列| Column H | 列H | Column W 列W

1 | 1 | Auditorium | 礼堂| Martin (INTERNAL) | 马丁(内部)| Match 比赛

2 | 2 | Auditorium | 礼堂| John | 约翰|

3 | 3 | Theatre | 剧院| Edward (INTERNAL) | 爱德华(内部)| Match 比赛

4 | 4 | Theatre | 剧院| George | 乔治| Match 比赛

Dim celA
Dim celB
    For Each celA In Range("D1:D50")
        For Each celB In Range("H1:H50")
                If InStr(1, celA.Value, "Auditorium") <> 0 And InStr(1, celB.Value, "INTERNAL") <> 0 Then celB.Offset(0, 10).Value = "Match"
        Next celB
    Next celA

You don't need your inner loop. 你不需要内循环。 Once you locate the word auditorium , just look across the same row for internal and set the value in column W if found. 找到“ 礼堂 ”一词后,只需查看内部的同一行,如果找到则设置W列中的值。

Dim celD
For Each celD In Range("D1:D50")
    If cbool(InStr(1, lcase(celD.Value), "auditorium")) then
        If cbool(InStr(1, lcase(celD.offset(0, 4).Value), "internal")) then
            celD.offset(0, 19) = "Match"
        end if
    end if
Next celD 

additional variant to already posted 已发布的其他变体

Dim celD As Range, SearchKey$
SearchKey = "*auditorium*internal*"
For Each celD In [D1:D50]
    If LCase(celD.Text & celD.Offset(, 4).Text) Like LCase(SearchKey) Then '.text instead of value|value2 is used to convert #errors into string (e.g. #N/A)
        celD.Offset(, 19) = "Match"
    End If
Next celD

you can also do that with AutoFilter() and thus avoid looping (explanations in comments): 你也可以使用AutoFilter()来做到这一点,从而避免循环(注释中的解释):

With Range("H1", Cells(Rows.Count, "D").End(xlUp)) 'reference columns "H:D" range from row 1 down to column D last not empty row
    .AutoFilter Field:=1, Criteria1:="Auditorium" 'filter referenced range on its first column (i.e. column "D") with value "Auditorium
    .AutoFilter Field:=5, Criteria1:="*INTERNAL*" 'filter referenced range on its fifth column (i.e. column "H") with value containing "INTERNAL"
    If Application.WorksheetFunction.Subtotal(103, .Columns(1)) > 1 Then .Resize(, 1).Offset(, 19).SpecialCells(xlCellTypeVisible).Value = "MATCH" 'if any "data" (i.e. from 2nd row downwards) rows matching criteria then write "MATCH" in corresponding rows of 19th column of referenced range (i.e. column "W")
    If .Cells(1, 1).Value = "Auditorium" And InStr(.Cells(1, 5), "INTERNAL") > 0 Then .Cells(1, 20).Value = "MATCH" 'check first ("Headers") rows too
End With
ActiveSheet.AutoFilterMode = False 'remove filters

should your data have a "header" row also, than you could omit the last code line before End With 如果您的数据也有一个“标题”行,那么您可以省略End With之前的最后一个代码行

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

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