如何使用正则表达式在Excel VBA中获取列号?

如何使用正则表达式在Excel VBA中获取列号?,vba,excel,Vba,Excel,我知道如何使用.Match、.Find和迭代返回列号(恶心) 是否可以使用regex更快地完成它?尝试Debug.Print单元格(5,7)。列,它将输出7 下面是一个.Find方法的示例,它将输出找到的单元格的列 Sub test_Eswemenasja() Dim FirstAddress As String, cF As Range, SearchedStr as String SearchedStr = "Cat" ActiveSheet.Range("A1").Activate Wi

我知道如何使用.Match、.Find和迭代返回列号(恶心)


是否可以使用regex更快地完成它?

尝试
Debug.Print单元格(5,7)。列
,它将输出
7

下面是一个
.Find
方法的示例,它将输出找到的单元格的列

Sub test_Eswemenasja()
Dim FirstAddress As String, cF As Range, SearchedStr as String
SearchedStr = "Cat"

ActiveSheet.Range("A1").Activate
With ActiveSheet.Cells
    'First, define properly the Find method
    Set cF = .Find(What:=SearchedStr, _
                After:=ActiveCell, _
                LookIn:=xlFormulas, _
                LookAt:=xlPart, _
                SearchOrder:=xlByColumns, _
                SearchDirection:=xlNext, _
                MatchCase:=False, _
                SearchFormat:=False)

    'If there is a result, keep looking with FindNext method
    If Not cF Is Nothing Then
        FirstAddress = cF.Address
        Do
            'Message with the column number!
            MsgBox "Found in column : " & cF.column
            Set cF = .FindNext(cF)
        'Look until you find again the first result
        Loop While Not cF Is Nothing And cF.Address <> FirstAddress
    End If
End With

End Sub
子测试_Eswemenasja()
Dim FirstAddress作为字符串,cF作为范围,SearchedStr作为字符串
SearchedStr=“Cat”
ActiveSheet.Range(“A1”).激活
使用ActiveSheet.Cells
'首先,正确定义Find方法
设置cF=.Find(What:=SearchedStr_
之后:=ActiveCell_
LookIn:=xl公式_
看:=xlPart_
SearchOrder:=xlByColumns_
SearchDirection:=xlNext_
MatchCase:=假_
SearchFormat:=False)
'如果有结果,请继续使用FindNext方法查找
如果不是,那么cF什么都不是
FirstAddress=cF.地址
做
'带有列号的消息!
在列中找到MsgBox:“&cF.column”
设置cF=.FindNext(cF)
“看看,直到你再次找到第一个结果
循环而非cF为Nothing且cF.Address为FirstAddress
如果结束
以
端接头

column()怎么样?
@Shmukko:错了!您可以在中使用正则表达式VBA@R3uK好吧,你说得对。但是你需要一个外部引用。@Shmukko:是的,就像很多VBA的东西一样。。。