2016-09-30 79 views
2

我想在子程序之間傳遞表單和範圍的名稱。以下引發「下標超出範圍」錯誤:如何將工作表和範圍作爲變量傳遞?

Sub This() 

    x = "Sheet1" 
    y = "D3" 

    MsgBox (x.Range(y).Value) 

End Sub 

這是a sample of my Project Explorer

enter image description here

+1

這基本上是你最後一個問題的重複:http://stackoverflow.com/q/39783461/293078。答案是您的工作表被稱爲「估價」,而不是「Sheet1」。 –

回答

0

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

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 
+0

我只知道如何通過它的'Name'來引用工作表,你怎麼通過它的'CodeName'來引用它? –

+0

@ Gary'sStudent在鏈接'VBAProject.Sheet1.Range(「A1」)'或者'Sheet1.Range(「A1」)' – Slai