简体   繁体   English

从多个图纸定义动态范围

[英]Defining Dynamic Ranges from Multiple Sheets

I'm looking to define several dynamic ranges from multiple sheets. 我正在寻找从多个图纸定义几个动态范围。 I'm getting the error 1004 "Application or Object definition error." 我收到错误1004“应用程序或对象定义错误”。 My code works for any range where the sheet is active, and I've been able to make it work by activating each sheet and then define my dynamic range. 我的代码适用于工作表处于活动状态的任何范围,并且我已经能够通过激活每个工作表然后定义我的动态范围来使其工作。 But if my spreadsheet gets to big this can really slow my program down. 但是,如果我的电子表格过大,这确实会使我的程序变慢。

Please note the following: 请注意以下事项:

  1. .CurrentRegion will not work as it will include many unnecessary rows and columns. .CurrentRegion将不起作用,因为它将包含许多不必要的行和列。 The report will include regions of repeating columns due to different areas of production. 该报告将包括由于生产区域不同而导致的重复列区域。

Is there a better method of defining my ranges to prevent from having to activate each sheet? 有没有更好的方法来定义我的范围以防止不得不激活每张图纸? Below is an example of some of the code I'm using. 下面是我正在使用的一些代码的示例。

Set Sony = ThisWorkbook.Worksheets("Report")
Set Prod = ThisWorkbook.Worksheets("Prod Report")

Prod.Activate
Set rng1 = Prod.Range(Range("C3"), Range("C3").End(xlDown).Offset(-1, 0))
Set rng2 = Prod.Range(Cells(1, 1), Cells(1, 52))
Sony.Activate
Set rng3 = Sony.Range(Range("B4"), Range("B4").End(xlDown).Offset(-1, 0).End(xlToRight).Offset(0, -1))

Qualify the ranges to refer to the specific sheet, without that they refer to the ActiveSheet . 限定范围以引用特定的表,而不是引用ActiveSheet This should apply also to the ranges that occur inside the parentheses; 应适用于括号内的范围; without that you'd have runtime errors. 否则,您将遇到运行时错误。

Set rng1 = Prod.Range("C3", Prod.Range("C3").End(xlDown).Offset(-1, 0))
'                           ^^^^^

Set rng2 = Prod.Range("A1", Prod.Cells(1, 52))
'                           ^^^^^

Set rng3 = Sony.Range("B4", Sony.Range("B4").End(xlDown).Offset(-1, 0).End(xlToRight).Offset(0, -1))
'                           ^^^^^

For rng2 , you could also use a simpler notation: 对于rng2 ,您还可以使用一个更简单的符号:

Set rng2 = Prod.Range("A1").Resize(1, 52)

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

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