excel 从给定变量中按行或列选择单元格范围

uxh89sit  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(191)

A1:A10是excel中范围的通用方式
我们能否将A1和A10存储在变量中,并像下面这样访问它

num1 = A1
num2 = A10
num1:num2?

这在excel vba中可能吗?
我尝试过此操作,但出现错误

Sub check()

Dim num1, num2 As Long
Dim rng As Range

num1 = Range("A1").Value
num2 = Range("A10").Value

Set rng = Range(num1 & ":" & num2)
Debug.Print (rng)
    
End Sub
rxztt3cl

rxztt3cl1#

看样子这就是你要找的东西?

Sub checkAsString()

    Dim rng As Range
    Dim num1 As String, num2 As String

    num1 = "A1"
    num2 = "A2"
    strAddress = Trim(num1 & ":" & num2)

    Set rng = Range(strAddress)
    
End Sub

或者,如果您确实想使用编号区域,可能是因为您想使用整行或整列。在这种情况下,请参阅下面的内容:

Sub checkAsLong()

    Dim rng As Range, address As Range
    Dim str1 As String, str2 As String
    Dim num1 As Long, num2 As Long

    str1 = "A1"
    str2 = "A2"

    num1 = CLng(Range(str1).Value)
    num2 = CLng(Range(str2).Value)

    'assuming you want to select whole Rows...
    Set rng = Range(Rows(num1), Rows(num2))

    'assuming you want to select whole Columns...
    Set rng = Range(Columns(num1), Columns(num2))

End Sub

相关问题