如何将工作表和范围作为变量传递? [英] How do I pass worksheet and ranges as variables?

查看:51
本文介绍了如何将工作表和范围作为变量传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Sub This()x = "工作表 1"y = "D3"MsgBox (x.Range(y).Value)结束子

这是

解决方案

您工作表的 .Name 属性估值;它的工作表 .CodeName 属性Sheet1.

dim x 作为字符串,y 作为字符串将 xx 作为工作表,将 yy 作为范围x = "估价"设置 xx = 工作表(x)y = "D3"设置 yy = xx.range(y)debug.print yy.address(0,0, external:=true) '<~~returns Valuation!D3 (with the workbook name)debug.print Sheet1.name '<~~从 CodeName 返回估值

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.

解决方案

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

这篇关于如何将工作表和范围作为变量传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆