简体   繁体   English

Excel VB-使用变量引用工作表索引吗?

[英]Excel VB - Refer to worksheet index using a variable?

I would like to use a single line of code to refer to the same cell but in different worksheets (within the same workbook). 我想使用一行代码来引用相同的单元格,但引用不同的工作表(在同一工作簿中)。 For example: 例如:

Dim SheetRef as Worksheet
SheetRef = Sheet6    
SheetRef.Range("E5").Value = "Please work!"

where SheetRef can be changed within VB to represent different worksheet index numbers (rather than sheet name). 在VB中可以更改SheetRef,以表示不同的工作表索引号(而不是工作表名称)。

I have tried many combinations, dim'd the variable as many different things, googled high and low and made no progress, any assistance very much appreciated! 我尝试了许多组合,将变量作为许多不同的事物进行了调暗,谷歌高低地搜索,没有取得任何进展,非常感谢任何帮助!

Many thanks, 非常感谢,

Martin. 马丁。

First, you need to use set to declare your sheet object equal to a sheet. 首先,您需要使用set来声明您的工作表对象等于工作表。 then, use the index notation sheets(i) 然后,使用索引符号表(i)

dim sheetref as worksheet
dim i as long

i = 2 'sheet index number

set sheetref = sheets(i)
sheetref.range("E5").value = "WORKS!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM