简体   繁体   English

Excel宏中的部分单元格(或字符串)匹配

[英]Partial cell(or string) match in excel macro

I am new to VBA and I would like to do a partial string (or cell) match between two sheets. 我是VBA的新手,我想在两张纸之间进行部分字符串(或单元格)匹配。

An example of Name1 would be "IT executive Sally Lim" Name1的一个示例是“ IT主管Sally Lim”

An example of Name2 would be "Sally Lim" Name2的一个示例是“ Sally Lim”

Name1 = Sheets("Work").Cells(RowName1, ColName1)
Name2 = Sheets("Roster").Cells(RowName2, ColName2)

'This condition doesn't work
If Name1 = "*" & Name2 & "*" Then
    'The "Name2" comes out with a compile error: Invalid Qualifier
    Name2.Font.Strikethrough
    Exit Do
Else
    End If

However, it is not working. 但是,它不起作用。 When I run the coding, either nothing happens or an error pops out. 当我运行编码时,没有任何反应或弹出错误。 Please help 请帮忙

Edited coding: 编辑编码:

If ShiftName Like "*" & CashName & "*" Then
    CashName.Font.Strikethrough = True

The strikeout portion has been solved and it does not shows the "Compile Error" anymore after I change my declaration from "string" to "range" as proposed by John Coleman. 我按照约翰·科尔曼的建议将声明从“字符串”更改为“范围”后,删除线部分已经解决,并且不再显示“编译错误”。

I tested by changing Name1 and Name2 to both Sally and then use the following condition for strikeout and it works. 我通过将Name1和Name2都更改为Sally进行了测试,然后将以下条件用于删除线,它可以正常工作。 I believed that it is the "*" that has made the condition unworkable. 我认为是使条件无法使用的“ *”。

If ShiftName Like CashName Then
    CashName.Font.Strikethrough = True

How can a partial match be done by changing the condition accordingly? 如何通过相应地更改条件来进行部分匹配?

SECOND EDIT: 第二编辑:

MY BAD! 我的错! I realised that my Name1 was in CAPTIALS. 我意识到我的Name1是大写的。

In addition to @MacroMan 's answer about using Like , you would also need to use Strikethrough correctly. 除了@MacroMan有关使用Like的答案外,您还需要正确使用Strikethrough It is a Boolean property which needs to be set to True: 这是一个布尔属性,需要设置为True:

If Name1 Like "*" & Name2 Then
    Name2.Font.Strikethrough = True
    Exit Do
Else
    End If

On Edit: 在编辑时:

Based on your expanded question, you could do something like this: 根据您提出的问题,您可以执行以下操作:

Dim Name1 As Range, Name2 As Range 'If you don't have this already declared

'then ... in the loop:

Set Name1 = Sheets("Work").Cells(RowName1, ColName1)
Set Name2 = Sheets("Roster").Cells(RowName2, ColName2)

If Name1.Value Like "*" & Name2.Value & "*" Then
    Name2.Font.Strikethrough = True
    Exit Do
Else
    End If

It isn't strictly necessary to use .Value on Range variables (the comparison using Like would work as expected without it) but it is considered by many to be good VBA coding style to be explicit when using range variables as opposed to relying on the default property of range objects. 并非绝对必要在范围变量上使用.ValueLike没有,则使用Like的比较将按预期进行工作),但许多人认为,当使用范围变量而不是依赖于变量时,它是一种很好的VBA编码风格。范围对象的默认属性。

You could also dispense with the variables Name1 and Name2 entirely: 您也可以完全省去变量Name1Name2

If Sheets("Work").Cells(RowName1, ColName1).Value Like "*" & Sheets("Roster").Cells(RowName2, ColName2).Value & "*" Then
   Sheets("Roster").Cells(RowName2, ColName2).Font.Strikethrough = True
    Exit Do
Else
    End If

A final remark: The Else followed immediately by End If is somewhat pointless. 最后的话: Else紧接着End If毫无意义。 Presumably your actual code does something in the else clause. 大概您的实际代码在else子句中做了什么。 If not -- just delete else entirely and have End If immediately after the Exit Do 如果没有-只是删除东西完全和有End If后立即Exit Do

Use the Like operator: 使用Like运算符:

If Name1 Like "*" & Name2 Then

You can use Like for specific pattern matching, but it also allows the wild character * 您可以将Like用于特定的模式匹配,但也可以使用通配符*

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

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