我正在尝试获取代码以检查整个列是否包含特定文本。
它永远是大写字母。此行之后有很多代码,但我只希望代码在满足以下条件时运行。我正在考虑的代码如下。
If ActiveSheet.Range("J:J").Text = "GENERAL" Then
但是,这没有任何回报,我尝试过Instr()
但无济于事。
什么是正确的语法?
您可以在一个范围内搜索;
if not ActiveSheet.Range("J:J").Find("GENERAL", MatchCase:=True) is nothing then
msgbox "found it!"
end if
FInd
有用法
Find(What, [After], [LookIn], [LookAt], [SearchOrder], [SearchDirection As XlSearchDirection = xlNext], [MatchCase], [MatchByte], [SearchFormat])
您应该指定匹配“GENERAL”应该是整个字符串(使用LookAt:=xlWhole
),还是部分匹配(即匹配“GENERAL COMMOTION”中的GENERAL使用LookAt:=xlPart
)
此外,通常最好使用一个Range
对象(rng1
如下),这样您就可以使用找到的对象。尽管在您的情况下,布尔测试足以运行或不运行您的进一步代码
部分匹配
Sub FindPartialString()
Dim rng1 As Range
'find partial match
Set rng1 = ActiveSheet.Range("J:J").Find("GENERAL", , xlValues, xlPart, , , True)
If Not rng1 Is Nothing Then
MsgBox "found in " & rng1.Address(0, 0)
Else
MsgBox "no found", vbCritical
End If
End Sub
整场比赛
Sub FindWholeString()
Dim rng1 As Range
'find exact match
Set rng1 = ActiveSheet.Range("J:J").Find("GENERAL", , xlValues, xlWhole, , , True)
If Not rng1 Is Nothing Then
MsgBox "found in " & rng1.Address(0, 0)
Else
MsgBox "no found", vbCritical
End If
End Sub