excel - 对象 'Union' 的方法 '_Global' 在同一工作表上的单元格上失败

标签 excel vba

据我所知,我已经正确声明了一切(尽管很复杂),我只需要一双新的眼睛。根据编译器,我的“MultiForm1”联合不正确:

Sub ShiftMaster()
'Declarations
Dim Mon As Range, Tue As Range, Wed As Range, Thu As Range, Fri As Range, Sat As Range, Sun As Range, _
 Multi1 As Range, Multi2 As Range, Multi3 As Range, Multi4 As Range, Multi5 As Range, Multi6 As Range, _
 DayNum As Range, MonFormOne As Range, MonFormTwo As Range, MonFormThree As Range, TueFormOne As Range, _
 TueFormTwo As Range, TueFormThree As Range, WedFormOne As Range, WedFormTwo As Range, WedFormThree As Range, _
 ThuFormOne As Range, ThuFormTwo As Range, ThuFormThree As Range, FriFormOne As Range, FriFormTwo As Range, _
 FriFormThree As Range, SatFormOne As Range, SatFormTwo As Range, SatFormThree As Range, SunFormOne As Range, _
 SunFormTwo As Range, SunFormThree As Range, ShiftNum As Range, MultiForm1 As Range, MultiForm2 As Range, _
 MultiForm3 As Range, MultiForm4 As Range, MultiForm5 As Range, MultiForm6 As Range, MultiForm7 As Range

Set Mon = Sheets("Production").Range("B3:F26")
Set Tue = Sheets("Production").Range("K3:O26")
Set Wed = Sheets("Production").Range("T3:X26")
Set Thu = Sheets("Production").Range("AC3:AG26")
Set Fri = Sheets("Production").Range("AL3:AP26")
Set Sat = Sheets("Production").Range("AU3:AY26")
Set Sun = Sheets("Production").Range("BD3:BH26")
Set Multi1 = Union(Mon, Tue, Wed, Thu, Fri, Sat, Sun)
Set Multi2 = Union(Tue, Wed, Thu, Fri, Sat, Sun)
Set Multi3 = Union(Wed, Thu, Fri, Sat, Sun)
Set Multi4 = Union(Thu, Fri, Sat, Sun)
Set Multi5 = Union(Fri, Sat, Sun)
Set Multi6 = Union(Sat, Sun)
Set DayNum = Sheets("Command Console").Range("J22")
Set MonFormOne = Sheets("Production").Range("H9")
Set MonFormTwo = Sheets("Production").Range("H17")
Set MonFormThree = Sheets("Production").Range("H25")
Set TueFormOne = Sheets("Production").Range("H33")
Set TueFormTwo = Sheets("Production").Range("H41")
Set TueFormThree = Sheets("Production").Range("H49")
Set WedFormOne = Sheets("Production").Range("H57")
Set WedFormTwo = Sheets("Production").Range("H65")
Set WedFormThree = Sheets("Production").Range("H73")
Set ThuFormOne = Sheets("Production").Range("H81")
Set ThuFormTwo = Sheets("Production").Range("H89")
Set ThuFormThree = Sheets("Production").Range("H97")
Set FriFormOne = Sheets("Production").Range("H105")
Set FriFormTwo = Sheets("Production").Range("H113")
Set FriFormThree = Sheets("Production").Range("H121")
Set SatFormOne = Sheets("Production").Range("H129")
Set SatFormTwo = Sheets("Production").Range("H137")
Set SatFormThree = Sheets("Production").Range("H145")
Set SunFormOne = Sheets("Production").Range("H153")
Set SunFormTwo = Sheets("Production").Range("H161")
Set SunFormThree = Sheets("Production").Range("H169")
Set ShiftNum = Sheets("Command Console").Range("J24")
**Set MultiForm1 = Union(MonFormOne, MonFormTwo, MonFormThree, TueFormOne, TueFormTwo, TwoFormThree, WedFormOne, WedFormTwo, WedFormThree, _
 ThuFormOne, ThuFormTwo, ThuFormThree, FriFormOne, FriFormTwo, FriFormThree, SatFormOne, SatFormTwo, SatFormThree, SunFormOne, SunFormTwo, SunFormThree)**
Set MultiForm2 = Union(TueFormOne, TueFormTwo, TwoFormThree, WedFormOne, WedFormTwo, WedFormThree, ThuFormOne, ThuFormTwo, ThuFormThree, _
 FriFormOne, FriFormTwo, FriFormThree, SatFormOne, SatFormTwo, SatFormThree, SunFormOne, SunFormTwo, SunFormThree)
Set MultiForm3 = Union(WedFormOne, WedFormTwo, WedFormThree, ThuFormOne, ThuFormTwo, ThuFormThree, FriFormOne, FriFormTwo, FriFormThree, _
 SatFormOne, SatFormTwo, SatFormThree, SunFormOne, SunFormTwo, SunFormThree)
Set MultiForm4 = Union(ThuFormOne, ThuFormTwo, ThuFormThree, FriFormOne, FriFormTwo, FriFormThree, SatFormOne, SatFormTwo, SatFormThree, _
 SunFormOne, SunFormTwo, SunFormThree)
Set MultiForm5 = Union(FriFormOne, FriFormTwo, FriFormThree, SatFormOne, SatFormTwo, SatFormThree, SunFormOne, SunFormTwo, SunFormThree)
Set MultiForm6 = Union(SatFormOne, SatFormTwo, SatFormThree, SunFormOne, SunFormTwo, SunFormThree)
Set MultiForm7 = Union(SunFormOne, SunFormTwo, SunFormThree)

'###########################################################################################################
'#                              Selects cells for format deletion, calls deleter                           #
'###########################################################################################################

Sheets("Production").Select
ActiveSheet.Unprotect
If DayNum = 1 Then
Multi1.Select
ElseIf DayNum = 2 Then
Multi2.Select
ElseIf DayNum = 3 Then
Multi3.Select
ElseIf DayNum = 4 Then
Multi4.Select
ElseIf DayNum = 5 Then
Multi5.Select
ElseIf DayNum = 6 Then
Multi6.Select
ElseIf DayNum = 7 Then
Sun.Select
Else
MsgBox ("There is a problem with the day number formula")
End If
Call BorderBlaster

'###########################################################################################################
'#                                          End Format deletion                                            #
'###########################################################################################################

'###########################################################################################################
'#                            Chooses latent formulas for deletion, calls deleter                          #
'###########################################################################################################

If DayNum = 1 Then
MultiForm1.Select
ElseIf DayNum = 2 Then
MultiForm2.Select
ElseIf DayNum = 3 Then
MultiForm3.Select
ElseIf DayNum = 4 Then
MultiForm4.Select
ElseIf DayNum = 5 Then
MultiForm5.Select
ElseIf DayNum = 6 Then
MultiForm6.Select
ElseIf DayNum = 7 Then
MultiForm7.Select
Else
MsgBox ("There is a problem with the day number formula")
End If

Call FormulaBlaster

'###########################################################################################################
'#                                          End Formula Deletion                                           #
'###########################################################################################################

Call BorderDamon

Call FormulaDamon

Call LastRights

End Sub
Sub BorderBlaster()
'Clears borders
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlDouble
        .ThemeColor = 5
        .TintAndShade = -0.499984740745262
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlDouble
        .ThemeColor = 9
        .TintAndShade = -0.499984740745262
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .ThemeColor = 9
        .TintAndShade = -0.499984740745262
        .Weight = xlThick
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlDouble
        .ThemeColor = 9
        .TintAndShade = -0.499984740745262
        .Weight = xlThick
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
    End With
End Sub
Sub FormulaBlaster()
'Clears formulas
Selection.ClearContents

End Sub
Sub BorderDamon()
'Some Code
End Sub
Sub FormulaDamon()
'Some Code
End Sub
Sub LastRights()
'Some Code
End Sub

显然代码不完整。有谁知道为什么会导致这种情况?

最佳答案

您需要进入工具/选项并选择需要变量声明的选项。这将放置 Option Explicit在您插入的任何新模块的开头。

如果你这样做了,你会很快发现你的错误,以及其他 Set 语句中的类似错误。

Set MultiForm1 = Union(MonFormOne, MonFormTwo, MonFormThree, TueFormOne, TueFormTwo, **TwoFormThree**, WedFormOne, WedFormTwo, WedFormThree, _
 ThuFormOne, ThuFormTwo, ThuFormThree, FriFormOne, FriFormTwo, FriFormThree, SatFormOne, SatFormTwo, SatFormThree, SunFormOne, SunFormTwo, SunFormThree)

关于excel - 对象 'Union' 的方法 '_Global' 在同一工作表上的单元格上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32147200/

相关文章:

excel - 如何在 Excel 的 Select Case 中使用多个条件

excel - 获取特定类别的最后可用日期

vb.net - SeriesCollection.Add() 和 SeriesCollection.NewSeries() 之间有什么细微的区别吗?

database - Access +VBA : Correct creation of database

ms-access - 如何在VBA IDE中使用 "Locals"窗口

excel - (MS Excel) 如果 sumifs 函数中的条件用引号引起来,如何自动增加该行?

java - 如何使用 Java Swing 创建 Excel 应用程序?

vba - 使 .xlsm 文件在使用 VBA 打开时不执行代码

excel - 在给定 Range 对象的 VBA 中循环遍历两个相同长度的单元格范围

VBA异常处理失败?