简体   繁体   English

如何检查单元格的值是否为另一个单元格的子值

[英]How to check if value of cell is a subvalue of another cell

I have two columns with and ID such as "TCG45436" in both of them. 我在两列中都有ID为“ TCG45436”的ID。 Column one includes a number in parenthesis like "TCG45436 (5)". 第一列的括号中有一个数字,例如“ TCG45436(5)”。 I need to check both columns to to see if the IDs match, and clear the cell contents of column two if they don't. 我需要检查两列以查看ID是否匹配,如果不匹配,请清除第二列的单元格内容。 The problem is the two won't match if column one includes the (5), but I only need the ID itself to match in both columns. 问题是,如果第一列包含(5),则两者将不匹配,但是我只需要ID本身在两列中都匹配即可。 I've done something similar in the past using substrings to find if columns 2 is a substring of column one, but I can't figure out how to apply that here. 过去我使用子字符串来完成类似的操作,以查找第2列是否是第1列的子字符串,但是我不知道如何在此处应用它。

Essentially, I want B30, and B32-B37 cleared in this snippet. 本质上,我希望在此片段中清除B30和B32-B37。

Excel ScreenShot

Sub TwoColumns()
Do Until ActiveCell.Value = ""
Column1 = ActiveCell.Value
Column2 = ActiveCell.Offset(0, 1).Value

If Column1 = Column2 Then 'needs to be If Column2 is equal to or a subvalue of Column1
Else
    ActiveCell.Offset(0, 1).ClearContents
End If
    ActiveCell.Offset(1, 0).Select
Loop
End Sub

Thanks 谢谢

您可能要使用InStr函数:

If InStr(0, Column1.Value, Column2.Value) <> 0 Then 'it's a substring.

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

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