InStr(1,cell.Value,“ - ”)似乎不适用于Not

时间:2022-03-19 00:06:04

I have a conditional that doesn't seem to work.

我有条件似乎不起作用。

If Not InStr(1, cell.Value, "-") Then
    'Do Something
Else
    'Do something else
End If

Where cell.Value are either numbers in a spreadsheet with a dash: "6621-123", or without a dash: "555321"

其中cell.Value是带有短划线的电子表格中的数字:“6621-123”,或没有短划线:“555321”

The first If let's both through and the Else is ignored. Any ideas why this isn't working?

第一个如果让我们通过,而Else被忽略。任何想法为什么这不起作用?

2 个解决方案

#1


9  

InStr returns 0 on no match (not -1 as VBA string indexes are 1 based) and not 0 is true (-1); so are all other possible values > 0 that can be returned.

InStr在不匹配时返回0(不是-1,因为VBA字符串索引是基于1)而不是0是真(-1);所有其他可能的值> 0都可以返回。

If InStr(1, cell.Value, "-") = 0 Then
    '// not present
Else
    '// present  

#2


0  

The first If let's both through and the Else is ignored.

第一个如果让我们通过,而Else被忽略。

I would think that if one of the samples' actual Range.Value property contained a hyphen (aka Chr(45) and the other did not then the behavior would not be the same for both of your samples regardless of whether you got the InStr function's syntax correct. What you have is enough to determine a True/False condition¹ although I typically use a CBool wrapper around the Instr to remind myself that its result is being used in a boolean fashion.

我认为如果其中一个样本的实际Range.Value属性包含一个连字符(也就是Chr(45)而另一个没有,那么无论你是否有InStr函数,你的两个样本的行为都不一样)语法正确。你所拥有的就足以确定一个真/假的条件¹尽管我通常在Instr周围使用一个CBool​​包装器来提醒自己它的结果是以布尔方式使用的。

I believe that your 'value' showing a hyphen is actually 6621123 not 6621-123 and that number formatting has been applied to the cell (e.g. custom number format of 0000-000. In this case, if you want to determine the existence of a hyphen in the displayed value use the Range.Text property.

我相信显示连字符的'值'实际上是6621123而不是6621-123并且该数字格式已应用于单元格(例如,0000-000的自定义数字格式。在这种情况下,如果您想确定是否存在显示值中的连字符使用Range.Text属性。

If CBool(InStr(1, cell.Text, "-")) Then
    'hyphen exists as seen on worksheet either in .Value or .Text
Else
    'hyphen does not exist as seen on worksheet
End If

¹ In VBA False is zero and for all intents and purposes, True is anything that is not False. Strictly speaking, a True in VBA resolves mathematically as (-1) while a worksheet TRUE resolves as 1.

¹在VBA中,False为零,对于所有意图和目的,True都是非False的。严格地说,VBA中的True在数学上解析为(-1)而工作表TRUE解析为1。

#1


9  

InStr returns 0 on no match (not -1 as VBA string indexes are 1 based) and not 0 is true (-1); so are all other possible values > 0 that can be returned.

InStr在不匹配时返回0(不是-1,因为VBA字符串索引是基于1)而不是0是真(-1);所有其他可能的值> 0都可以返回。

If InStr(1, cell.Value, "-") = 0 Then
    '// not present
Else
    '// present  

#2


0  

The first If let's both through and the Else is ignored.

第一个如果让我们通过,而Else被忽略。

I would think that if one of the samples' actual Range.Value property contained a hyphen (aka Chr(45) and the other did not then the behavior would not be the same for both of your samples regardless of whether you got the InStr function's syntax correct. What you have is enough to determine a True/False condition¹ although I typically use a CBool wrapper around the Instr to remind myself that its result is being used in a boolean fashion.

我认为如果其中一个样本的实际Range.Value属性包含一个连字符(也就是Chr(45)而另一个没有,那么无论你是否有InStr函数,你的两个样本的行为都不一样)语法正确。你所拥有的就足以确定一个真/假的条件¹尽管我通常在Instr周围使用一个CBool​​包装器来提醒自己它的结果是以布尔方式使用的。

I believe that your 'value' showing a hyphen is actually 6621123 not 6621-123 and that number formatting has been applied to the cell (e.g. custom number format of 0000-000. In this case, if you want to determine the existence of a hyphen in the displayed value use the Range.Text property.

我相信显示连字符的'值'实际上是6621123而不是6621-123并且该数字格式已应用于单元格(例如,0000-000的自定义数字格式。在这种情况下,如果您想确定是否存在显示值中的连字符使用Range.Text属性。

If CBool(InStr(1, cell.Text, "-")) Then
    'hyphen exists as seen on worksheet either in .Value or .Text
Else
    'hyphen does not exist as seen on worksheet
End If

¹ In VBA False is zero and for all intents and purposes, True is anything that is not False. Strictly speaking, a True in VBA resolves mathematically as (-1) while a worksheet TRUE resolves as 1.

¹在VBA中,False为零,对于所有意图和目的,True都是非False的。严格地说,VBA中的True在数学上解析为(-1)而工作表TRUE解析为1。