简体   繁体   English

在VBA之外引用ActiveSheet对象

[英]Reference to ActiveSheet Object Outside of VBA

I am looking to run the following tasks: 我希望运行以下任务:

1: Create a template worksheet with dynamic charts (ie the charts are linked to a data range that frequently change in size) 1:使用动态图表创建模板工作表(即,图表链接到大小经常变化的数据范围)

2: The template is copied several times into new worksheets, with each worksheet automatically renamed using code that cycles through the PivotItems in another worksheet. 2:模板多次复制到新的工作表中,每个工作表使用循环在另一个工作表中的PivotItems的代码自动重命名。

3: Date from a PivotTable is then copied through to these duplicated worksheet, with the dynamic tables in each worksheet now reflecting the data copied through to it. 3:然后,将数据透视表中的日期复制到这些重复的工作表中,每个工作表中的动态表现在都反映了复制到其中的数据。

I have coded most of these tasks although am encountering the following issue. 尽管遇到以下问题,但我已对大多数任务进行了编码。 Dynamic charts are created through reference to named ranges that are defined using the OFFSET and COUNTA functions. 动态图表是通过引用使用OFFSET和COUNTA函数定义的命名范围创建的。 Each time the template duplicates, I have set named ranges to be defined in the new worksheet using the following code: 每次模板重复时,我都会使用以下代码设置要在新工作表中定义的命名范围:

ActiveWorkbook.ActiveSheet.Names.Add Name:="Label", RefersToR1C1:= _
    "=OFFSET(ActiveSheet!R46C3,1,0,COUNTA(ActiveSheet!R46C3:R69C3)-2)"
ActiveWorkbook.ActiveSheet.Names("Label").Comment = ""

ActiveWorkbook.ActiveSheet.Names.Add Name:="YTD2012", RefersToR1C1:= _
    "=OFFSET(ActiveSheet!R46C5,1,0,COUNTA(ActiveSheet!R46C3:R69C3)-2)"
ActiveWorkbook.ActiveSheet.Names("YTD2012").Comment = ""

ActiveWorkbook.ActiveSheet.Names.Add Name:="YTD2013", RefersToR1C1:= _
    "=OFFSET(ActiveSheet!R46C6,1,0,COUNTA(ActiveSheet!R46C3:R69C3)-2)"
ActiveWorkbook.ActiveSheet.Names("YTD2013").Comment = ""

[I am using clustered column charts, hence the requirement for two named ranges relating to data, and one relating to data labels] [我使用的是聚集的柱状图,因此需要两个与数据有关的命名范围,而一个与数据标签有关的范围]

The ActiveSheet object is required given that the worksheet names will differ each time I run the report. 鉴于每次我运行报告时工作表名称都会不同,因此需要ActiveSheet对象。 However, when selecting the data source for the chart on the template I cannot make reference to ActiveSheet given this is a VBA object. 但是,在选择模板上图表的数据源时,鉴于这是一个VBA对象,因此我无法引用ActiveSheet。 I would rather keep the charts pre-formatted on the template (as opposed to create them each time through VBA) given their formatting and positioning. 考虑到图表的格式和位置,我宁愿将图表预先设置在模板上(而不是每次通过VBA创建图表)。 Is there any way to make reference to ActiveSheet or something similar in Excel, or is the only solution to have the charts created for each worksheet in VBA? 有没有什么方法可以引用ActiveSheet或Excel中的类似方法,或者是在VBA中为每个工作表创建图表的唯一解决方案?

If you don't include the worksheet name it will default to the activesheet: 如果您不包括工作表名称,它将默认为活动表:

ActiveWorkbook.ActiveSheet.Names.Add Name:="Label", RefersToR1C1:= _
    "=OFFSET(R46C3,1,0,COUNTA(R46C3:R69C3)-2)"

If you did want to use the sheet name then it would be ActiveSheet.Name 如果确实要使用工作表名称,则它将为ActiveSheet.Name

With ActiveWorkbook.ActiveSheet
    .Names.Add Name:="Label", RefersToR1C1:= _
    Replace("=OFFSET('<nm>'!R46C3,1,0,COUNTA('<nm>'!R46C3:R69C3)-2)","<nm>", .Name)
End With

Need to add single quotes if the sheet names could contain spaces. 如果工作表名称可能包含空格,则需要添加单引号。

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

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