作者:手机用户2602898555 | 来源:互联网 | 2023-05-31 10:35
作为检查空白单元格的最后一步,我创建了一个要在检查中使用的范围。该范围基于一些固定集合,并且集合如何组合在一起是可变的。除了最后一个,所有的application.union都可以正常工作。我在模块中全局定义了所有变量,我尝试采用子特定的变量并仅在我的子变量中对其进行定义。我确保删除一个尚未完成的集,因为它可能是空的。我仍然收到无效过程错误。
公共变量在Module1中。在打开的工作簿上有一个子程序,它定义了要使用的范围。这是保存之前运行的子程序:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean,Cancel As Boolean)
Dim Drive As Range
Dim Stage As Range
Dim Required As Range
'Create the final range of cells for checking
If Module1.DriveChk = "G" Then
Set Drive = Module1.Engine
ElseIf Module1.DriveChk = "E" Then
Set Drive = Module1.Motor
End If
If Module1.StageChk = 1 Then
Set Stage = Module1.Stage1
End If
If Module1.StageChk = 2 Then
Set Stage = Application.Union(Module1.Stage1,Module1.Stage2)
End If
If Module1.StageChk = 3 Then
Set Stage = Application.Union(Module1.Stage1,Module1.Stage2,Module1.Stage3)
End If
Set Required = Application.Union(Module1.Fixed,Drive)
'Check if all required cells are filled in
If WorksheetFunction.CountA(Required) Cancel = True
MsgBox "Please Completed Shaded Cells!",vbOK + vbExclamation,"SAVE CANCELLED"
End If
'Set the report date before saving
Range("AG60") = Format(Now(),"mm-dd-yyyy hh:mm:ss AM/PM")
'Old code probably to ensure saving macro enabled that is currently commented out
' Dim sFN As String
' If SaveAsUI Then
' Cancel = True
' sFN = Application.GetSaveAsFilename(,_
' fileFilter:="Excel Macro-Enabled Workbooks (*.xlsm),*.xlsm")
' Debug.Print sFN
' If sFN <> "False" Then _
' ThisWorkbook.SaveAs sFN,xlOpenXMLWorkbookMacroEnabled
'End If
End Sub
在需要设置= Application.Union(Module1.Fixed,Drive)上发生错误
如果需要,我可以发布其他代码。
我试图合并一个“无”的范围,该范围总是与Application.Union一起引发错误
添加以下代码:
Debug.Print Module1.Fixed.Address,Module1.Fixed.Parent.Name
Debug.Print Drive.Address,Drive.Parent.Name
帮助我进行故障排除并查看发生了什么情况。