如何在Excel中使用VBA / Macro获取正则expression式匹配的列号

我想要得到像A:1的单元格编号,find使用正则expression式的每个匹配,并将其存储在同一个Excel文件中的当前旁边的工作表上。 有没有可能在Excel中实现。 由于less数例子,我尝试返回匹配find真/假。

Sub Sample() Dim ws As Worksheet Dim aCell As Range Set ws = ThisWorkbook.Sheets("Sheet1") With ws Set aCell = .Columns(2).Find(What:="Custom ", LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then aCell.Value = "Test" Else MsgBox "Not Found" End If End With End Sub 

这是我试过的样品!

尝试这个

 Sub Sample() Dim ws As Worksheet Dim aCell As Range Set ws = ThisWorkbook.Sheets("Sheet1") With ws Set aCell = .Columns(2).Find(What:="Custom ", LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then Worksheets("Sheet2").Range("A" & Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1).Value = aCell.Address Else MsgBox "Not Found" End If End With End Sub 

如果你想所有的比赛下面尝试

 Sub Sample() Dim ws As Worksheet Dim aCell As Range Set ws = ThisWorkbook.Sheets("Sheet1") lastrow = Range("B" & Rows.Count).End(xlUp).Row With ws For i = 1 To lastrow If InStr(Range("B" & i), "Custom ") > 0 Then Worksheets("Sheet2").Range("A" & Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1).Value = Range("B" & i).Address End If Next i End With End Sub