如何在范围引用中的单元格中引用值

时间:2019-02-18 22:20:00

标签: excel vba

这是针对excel中的VBA的。我知道我们定义了一个类似于Range(“ B5”)的范围。但是,我需要用户输入字母以表示其情况下的列和起始行。因此,就像您将B放入B1,将5放入B2。我无法让Range(B1,B2)工作。谢谢您的帮助!

2 个答案:

答案 0 :(得分:0)

改为使用Cells()。

' equivalent of cells(5, "B") or range("B5")
i = cells(range("B2").value, range("B1").value).value

'alternate for range("B5") through string concatenation
i = range(range("B1").value & range("B2").value).value

'one more
i = intersect(rows(range("B2").value), columns(range("B1").value)).value

答案 1 :(得分:0)

Option Explicit

Sub test()

   Dim Column As Long, Row As Long

    With ThisWorkbook.Worksheets("Sheet1")

        'Check if both fields (B1 & C1) are not empty
        If .Range("B1").Value <> "" And .Range("C1").Value <> "" Then
            'Convert letter to number
            Column = .Range(.Range("B1").Value & 1).Column 'or you can directly use Column = .Range("B1").Value and change Column variable from Long to String
            Row = .Range("C1").Value
            'Select the cell you refer to
            .Cells(Row, Column).Select
        Else: MsgBox "Please fill both B1 & C1 fields."
        End If

    End With

End Sub