使用VBA宏在excel行中搜索字符串的完全匹配

Man*_*aha 7 excel vba excel-vba

如何在excel中的一个特定行中搜索字符串?我在long类型变量中有行索引.

Dim rowIndex As Long
rowIndex = // some value being set here using some code.
Run Code Online (Sandbox Code Playgroud)

现在我需要检查行中是否存在特定值,索引是否为rowIndex.

如果匹配,我需要获取第一个匹配单元格的列索引.

我尝试过使用Match函数,但我不知道如何传递rowIndex变量来代替单元格范围.

Dim colIndex As Long    
colIndex = Application.Match(colName, Range("B <my rowIndex here>: Z <my rowIndex here>"), 0)
Run Code Online (Sandbox Code Playgroud)

Mat*_*rum 14

试试这个:

Sub GetColumns()

Dim lnRow As Long, lnCol As Long

lnRow = 3 'For testing

lnCol = Sheet1.Cells(lnRow, 1).EntireRow.Find(What:="sds", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column

End Sub
Run Code Online (Sandbox Code Playgroud)

可能最好不要将colIndex和rowIndex用作变量名,因为它们已经在Excel对象库中提到过了.

  • 你需要用 `xlWhole` 替换 `xlPart` 才能找到完全匹配的 (2认同)