How do I pass worksheet and ranges as variables?

主宰稳场 提交于 2019-12-13 16:01:16

问题


I want to pass the names of sheets and ranges between subroutines. The following throws a "Subscript out of range" error:

Sub This()

    x = "Sheet1"
    y = "D3"

    MsgBox (x.Range(y).Value)

End Sub

This is a sample of my Project Explorer.


回答1:


Your worksheet's .Name property is Valuation; its Worksheet .CodeName property is 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


来源:https://stackoverflow.com/questions/39800873/how-do-i-pass-worksheet-and-ranges-as-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!