简体   繁体   English

Excel VBA在混合数据时检查单元格是否包含字符串

[英]Excel VBA check if a cell contains a string when data is mixed

I have some data that contains both numeric and string values 我有一些包含数字和字符串值的数据

Name Code 

Bob   1

Alice 2

Bob   33AL

Dan   AFAL

Alert  AL99

Bob    ALP

I want to select names where the "Code" column contains "AL" somewhere. 我想选择“代码”列中某处包含“ AL”的名称。

If I do 如果我做

 If InStr(0, Cells(j, Code_column).Value, "AL", vbTextCompare) <> 0 Then

then my loop doesn't run because of the numeric values at some points in the column. 则由于该列中某些点的数字值,我的循环无法运行。

I have tried 我努力了

 If InStr(0, CStr(Cells(j, Code_column).Value), "AL", vbTextCompare) <> 0 Then

but I am getting the same error. 但是我遇到了同样的错误。 What should I try? 我该怎么办? A pre-loop checking if it is numeric? 循环前检查它是否为数字?

In code something like this for data in Columns A:B of the active sheet 对于活动工作表的A:B列中A:B数据,使用类似这样的代码

You can do the same thing manually without code 您无需代码即可手动执行相同的操作

Then in both cases work with the output (plus then reverse the logic for your second criteria) 然后在两种情况下都使用输出(加上然后反转第二条条件的逻辑)

Sub CookHooker()
Dim rng As Range
ActiveSheet.AutoFilterMode = False
Set rng1 = Range([a1], Cells(Rows.Count, "B").End(xlUp))
rng1.AutoFilter Field:=2, Criteria1:="=*AL*", Operator:=xlAnd
End Sub

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

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