我在代码中调试Error 450时遇到问题。对于17个范围,代码运行正常,但是当添加更多范围时,它会显示错误450参数数量错误或属性分配无效。请看一下代码。第一行突出显示,并在显示错误时显示“联盟”。
Sub Set_PrintArea()
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rng4 As Range
Dim rng5 As Range
Dim rng6 As Range
Dim rng7 As Range
Dim rng8 As Range
Dim rng9 As Range
Dim rng10 As Range
Dim rng11 As Range
Dim rng12 As Range
Dim rng13 As Range
Dim rng14 As Range
Dim rng15 As Range
Dim rng16 As Range
Dim rng17 As Range
Dim rng18 As Range
Dim rng19 As Range
Dim rng20 As Range
Dim rng21 As Range
Dim rng22 As Range
Dim rng23 As Range
Dim rng24 As Range
Dim rng25 As Range
Dim rng26 As Range
Dim rng27 As Range
Dim rng28 As Range
Dim rng29 As Range
Dim rng30 As Range
Dim rng31 As Range
Dim rng32 As Range
Dim rng33 As Range
Dim rng34 As Range
Dim rng35 As Range
With Sheets("Performance")
Set rng1 = .Range("$A$1:$U$13")
Set rng2 = .Range("$B$15:$Z$52")
Set rng3 = .Range("$B$55:$Z$92")
Set rng4 = .Range("$B$95:$Z$132")
Set rng5 = .Range("$B$135:$Z$172")
Set rng6 = .Range("$B$175:$Z$212")
Set rng7 = .Range("$B$215:$Z$252")
Set rng8 = .Range("$B$255:$Z$292")
Set rng9 = .Range("$B$295:$Z$332")
Set rng10 = .Range("$B$335:$Z$372")
Set rng11 = .Range("$B$374:$Z$407")
Set rng12 = .Range("$B$410:$Z$443")
Set rng13 = .Range("$B$446:$Z$479")
Set rng14 = .Range("$B$482:$Z$515")
Set rng15 = .Range("$B$518:$Z$551")
Set rng16 = .Range("$B$554:$Z$587")
Set rng17 = .Range("$B$590:$S$610")
Set rng18 = .Range("$B$613:$V$642")
Set rng19 = .Range("$B$650:$U$662")
Set rng20 = .Range("$B$664:$Z$701")
Set rng21 = .Range("$B$704:$Z$741")
Set rng22 = .Range("$B$744:$Z$781")
Set rng23 = .Range("$B$784:$Z$821")
Set rng24 = .Range("$B$824:$Z$861")
Set rng25 = .Range("$B$864:$Z$901")
Set rng26 = .Range("$B$904:$Z$941")
Set rng27 = .Range("$B$944:$Z$981")
Set rng28 = .Range("$B$984:$Z$1021")
Set rng29 = .Range("$B$1023:$AD$1066")
Set rng30 = .Range("$B$1069:$AD$1112")
Set rng31 = .Range("$B$1115:$AD$1158")
Set rng32 = .Range("$B$1161:$AD$1204")
Set rng33 = .Range("$B$1207:$AD$1250")
Set rng34 = .Range("$B$1253:$AD$1296")
Set rng35 = .Range("$B$1299:$S$1323")
With .PageSetup
.PrintArea = Union(rng1, rng2, rng3, rng4, rng5, rng6, rng7, rng8, rng9, rng10, rng11, rng12, rng13, rng14, rng15, rng16, rng17, rng18, rng19, rng20, rng21, rng22, rng23, rng24, rng25, rng26, rng27, rng28, rng29, rng30, rng31, rng32, rng33, rng34, rng35).Address
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
.PrintPreview
End With
End Sub
提前致谢。
答案 0 :(得分:2)
根据msdn,http://msdn.microsoft.com/en-us/library/office/ff834621(v=office.14).aspx
Union
只能接受30个参数
语法
expression .Union(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30)
要解决此问题,您可以将35个范围拆分为2组。 例如
union1 = union(rng1, rng2, ... , rng30)
union2 = union(rng31, rng32, ... , rng35)
unionFinal = union(union1, union2)
答案 1 :(得分:2)
您在Excel中遇到了两个限制:Union
的最大参数(正如其他人所说)和PrintArea
的字符串长度限制为255个字符。
您的打印区域地址是<如果不使用绝对寻址,则为255;
试试这个
Sub Demo()
ReDim rng(1 To 35)
Dim rngPrintArea As Range
Dim i As Long
With Sheets("Performance")
Set rng(1) = .Range("$A$1:$U$13")
Set rng(2) = .Range("$B$15:$Z$52")
Set rng(3) = .Range("$B$55:$Z$92")
Set rng(4) = .Range("$B$95:$Z$132")
Set rng(5) = .Range("$B$135:$Z$172")
Set rng(6) = .Range("$B$175:$Z$212")
Set rng(7) = .Range("$B$215:$Z$252")
Set rng(8) = .Range("$B$255:$Z$292")
Set rng(9) = .Range("$B$295:$Z$332")
Set rng(10) = .Range("$B$335:$Z$372")
Set rng(11) = .Range("$B$374:$Z$407")
Set rng(12) = .Range("$B$410:$Z$443")
Set rng(13) = .Range("$B$446:$Z$479")
Set rng(14) = .Range("$B$482:$Z$515")
Set rng(15) = .Range("$B$518:$Z$551")
Set rng(16) = .Range("$B$554:$Z$587")
Set rng(17) = .Range("$B$590:$S$610")
Set rng(18) = .Range("$B$613:$V$642")
Set rng(19) = .Range("$B$650:$U$662")
Set rng(20) = .Range("$B$664:$Z$701")
Set rng(21) = .Range("$B$701:$Z$741")
Set rng(22) = .Range("$B$744:$Z$781")
Set rng(23) = .Range("$B$784:$Z$821")
Set rng(24) = .Range("$B$824:$Z$861")
Set rng(25) = .Range("$B$864:$Z$901")
Set rng(26) = .Range("$B$904:$Z$941")
Set rng(27) = .Range("$B$944:$Z$981")
Set rng(28) = .Range("$B$984:$Z$1021")
Set rng(29) = .Range("$B$1023:$AD$1066")
Set rng(30) = .Range("$B$1069:$AD$1112")
Set rng(31) = .Range("$B$1115:$AD$1158")
Set rng(32) = .Range("$B$1161:$AD$1204")
Set rng(33) = .Range("$B$1207:$AD$1250")
Set rng(34) = .Range("$B$1253:$AD$1296")
Set rng(35) = .Range("$B$1299:$S$1323")
Set rngPrintArea = rng(1)
For i = 2 To 35
Set rngPrintArea = Union(rngPrintArea, rng(i))
Next
With .PageSetup
.PrintArea = rngPrintArea.Address(False, False)
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
End With
End Sub
答案 2 :(得分:1)
...较短
Sub Set_PrintArea()
Dim rng As Range, x As Long, arr
'add all your ranges here...
arr = Array("$A$1:$U$13", "$B$15:$Z$52", "$B$55:$Z$92", "$B$95:$Z$132")
With Worksheets("Performance")
Set rng = .Range(arr(LBound(arr)))
For x = LBound(arr) + 1 To UBound(arr)
Set rng = Application.Union(rng, .Range(arr(x)))
Next x
With .PageSetup
.PrintArea = rng.Address
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
.PrintPreview
End With
End Sub