如何在Excel VBA中的多个工作表中选择相同的范围?

时间:2018-08-01 17:50:57

标签: excel-vba

我只是尝试使用并集方法在多个工作表中选择相同的范围。我在代码的第16行收到“所需的对象(错误424)”。我不知道自己在做什么错,并尝试了许多不同的方法!

Sub MultipleRange()
 TheRange = "C6:D18,C22:D31,C35:D40,C44:D48,C52:D62,C66:D71,C75:D80,H20:I27,H31:I39,H43:I48,H52:I60,H64:I70,H75:I79"
 Dim r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, Rangey As Range
 Set r1 = Sheets("Jan").Range(TheRange)
 Set r2 = Sheets("Feb").Range(TheRange)
 Set r3 = Sheets("Mar").Range(TheRange)
 Set r4 = Sheets("Apr").Range(TheRange)
 Set r5 = Sheets("May").Range(TheRange)
 Set r6 = Sheets("Jun").Range(TheRange)
 Set r7 = Sheets("Jul").Range(TheRange)
 Set r8 = Sheets("Aug").Range(TheRange)
 Set r9 = Sheets("Sep").Range(TheRange)
 Set r10 = Sheets("Oct").Range(TheRange)
 Set r11 = Sheets("Nov").Range(TheRange)
 Set r12 = Sheets("Dec").Range(TheRange)
 Set Rangey = xl.Application.Union(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12)
 Rangey.Select
 Rangey.Activate
End Sub

1 个答案:

答案 0 :(得分:0)

这就是我想出的:

Sub ClearCells() If MsgBox("This will clear all of your monthly values! Are you sure?", vbYesNo) = vbNo Then Exit Sub TheRange = "C6:D18,C22:D31,C35:D40,C44:D48,C52:D62,C66:D71,C75:D80,H20:I27,H31:I39,H43:I48,H52:I60,H64:I70,H75:I79,H3:H5,H9:H11" Dim Sh As Worksheet For Each Sh In Sheets(Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) With Sh.Range(TheRange) '- ".Value= 0" OR ".ClearContents" .ClearContents End With Next MsgBox ("Sequence Complete!") End Sub