如何将工作表和范围作为变量传递?

时间:2016-09-30 22:18:54

标签: excel vba excel-vba range worksheet

我想在子程序之间传递工作表和范围的名称。以下内容将"下标超出范围"错误:

Sub This()

    x = "Sheet1"
    y = "D3"

    MsgBox (x.Range(y).Value)

End Sub

这是a sample of my Project Explorer

enter image description here

1 个答案:

答案 0 :(得分:0)

您的工作表.Name property 估值;其工作表.CodeName property Sheet1

dim x as string, y as string
dim xx as worksheet, yy as range

x = "Valuation"
set xx = worksheets(x)
y = "D3"
set yy = xx.range(y)
debug.print yy.address(0,0, external:=true)  '<~~returns Valuation!D3 (with the workbook name)

debug.print Sheet1.name    '<~~returns Valuation from CodeName