VBA,如何在由两列组成的命名范围中查找值

时间:2016-03-18 12:36:29

标签: excel vba excel-vba

我在Excel工作表下有一个命名范围,由两列组成( 第一个用于缩写,第二个用于名称)

我要求的是循环遍历此命名范围或使用.find()来捕获第一列中的值并在seconde列上获得相反的值。

任何帮助?

2 个答案:

答案 0 :(得分:0)

我找到了一种方法。

Dim rng As Range
Dim row As Range
Dim cell As Range

Set rng = Range("named range")

For Each row In rng.Rows
  For Each cell In row.Cells
    If cell = "RYO" Then
    MsgBox cell
    MsgBox cell.Next
    End If

  Next cell
Next row

答案 1 :(得分:0)

这是我的回答

Sub findTwoColumns()
    Dim f As Range
    Dim firstAddress
    Dim myRng As Range
    Set myRng = Range("myRange")
    With myRng
        Set f = .Find("JNI", LookIn:=xlValues, LookAt:=xlWhole)
        If Not f Is Nothing Then
            firstAddress = f.Address
            Do
                'msgbox f.Value   'here if you want o use the found value.
                'msgbox f.address 'here if you want o use the found address.
                MsgBox f.Offset(0, 1).Value 'take the value of the next cell
                Set f = .FindNext(f)
            Loop While Not f Is Nothing And f.Address <> firstAddress
        End If
    End With
End Sub

你可以采用缩写(JNI)的值作为参数。