完全公开,我已经用几种语言完成了基本编程,但这是我经过很长时间才将脚趾放回VBA中。我可能错过了非常基本的概念或语法...
该代码的目的是在保存之前检查一组单元格,以确保它们以excel伪格式完成。我将各个单元格范围定义为公共范围变量,并根据复选框和单选按钮将各个子集合并在一起。
我想使用复选框click子来设置一些用于此联合的变量,并自动调整必须完成的空白单元格的条件格式。那就是我遇到的一个问题,在Click Sub中无法识别公共变量。这可能是一个简单的变量定义错误,但我希望对此有所帮助。
我得到一个:
在复选框子菜单中的<马达>上按运行时错误“ 91”:
对象变量或未设置块变量
。如果我明确地在一个点上调出范围,则在下一台电机上也会出现相同的错误。
用于定义变量的模块代码:
Option Explicit
Public DriveChk As String
Public StageChk As Integer
Public Fixed As Range
Public Motor As Range
Public Engine As Range
Public Stage1 As Range
Public Stage2 As Range
Public Stage3 As Range
Public Required As Range
Public Stage As Range
Public Drive As Range
工作簿代码
Private Sub Workbook_Open()
'Ensure that the workbook stays protected but that the VBA script has full access to change cells.
ThisWorkbook.Worksheets("Field Service Report").Protect Password:="xxxxx", UserInterfaceOnly:=True
'Message to field service rep
MsgBox "Please complete FSR on the day of service." & vbNewLine & "All fields highlighted in red must be completed in order to save." & vbNewLine & "Please submit completed FSR to xxxxx", vbOK + vbInformation, "REMINDERS"
'Set all of the cell check and formatting ranges
Set Fixed = ThisWorkbook.Worksheets("Field Service Report").Range("E6,Q6,Z6,J7,AB7,J8,D28,O28,D29,A46,A58,F58,L58,P58,U57,V58,Y58")
Set Motor = ThisWorkbook.Worksheets("Field Service Report").Range("Z28,AI28,Z29,AF29,AL29,Z30,AI30,AB31")
Set Engine = ThisWorkbook.Worksheets("Field Service Report").Range("D15,M15,T15,AG15,AL15,T17,AB17,AE17,J23,J24,J25,AB20,AB21,AB22,AB23,AB24,AB25,AL20,AL21,AL22,AL23,AL24,AL25")
Set Stage1 = ThisWorkbook.Worksheets("Field Service Report").Range("I38,O38,AC38,AI38,I39,L39,O39,R39,AC39,AF39,AI39,AL39")
Set Stage2 = ThisWorkbook.Worksheets("Field Service Report").Range("I40,O40,AC40,AI40,I41,L41,O41,R41,AC40,AI40,AC41,F41,AI41,AL41")
Set Stage3 = ThisWorkbook.Worksheets("Field Service Report").Range("I42,O42,AC42,AI42,I43,O43,R43,AC43,AI43,AL43")
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Create the final range of cells for checking
If DriveChk = "G" Then
Set Drive = Engine
ElseIf DriveChk = "E" Then
Set Drive = Motor
End If
If StageChk = 1 Then
Set Stage = Stage1
End If
If StageChk = 2 Then
Set Stage = Application.Union(Stage1, Stage2)
End If
If StageChk = 3 Then
Set Stage = Application.Union(Stage1, Stage2, Stage3)
End If
Set Required = Application.Union(Fixed, Drive, Stage)
'Check if all required cells are filled in
If WorksheetFunction.CountA(Required) < Required.Count Then
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
Sheet Code
Option Explicit
Private Sub CheckBox26_Click()
If CheckBox26.Value = True Then
'MsgBox text to verify sub is working
MsgBox "CB26 is true"
DriveChk = "E"
Motor.FormatConditions _
.Delete
With Motor.FormatConditions _
.Add(Type:=xlBlanksCondition)
.Interior.ColorIndex = 30
.StopIfTrue = True
End With
Else
'MsgBox text to verify sub is working
MsgBox "CB26 is false"
DriveChk = "Z"
Motor.FormatConditions _
.Delete
With Motor.FormatConditions _
.Add(Type:=xlBlanksCondition)
.Interior.ColorIndex = 5
.StopIfTrue = True
End With
End If
End Sub
感谢您的帮助!抱歉,我无法完全理解使长命令无法包装的简写。我试图弄清楚,我的代码无法正常工作。
答案 0 :(得分:0)
您的模块变量是公用的,但仍在模块本地,因此应在模块名称前添加
dist