如何消除VBA中未定义的子或函数错误



我已经在excel中编写了此VBA代码,在将其保存到此工作簿中时,出现子或函数未定义错误,偏移线突出显示。我是VBA的新手,我不知道如何调试它,请帮我解决这个问题。

感谢

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
'######################################################################################
'Employee Name Validation Started
'######################################################################################
Activate
Range("B2").Select
exceptionCount = 0
Do Until ActiveCell.Address = "$B$10"
' Check if the Employee Name is Not Null
If IsEmpty(ActiveCell.Value) Then
totalExceptions = 1
exceptionCount = 1
Interior.ColorIndex = 6 'Highlight with Yellow Color
Else
Interior.ColorIndex = 15 'Retain Grey Color
End If
Offset(1, 0).Select
Loop
If exceptionCount = 1 Then
exceptionString = exceptionString + vbCrLf & "- Employee Name Cannot be Empty"
End If
'#######'Employee Name Validation Completed '##############################################

End Sub

您忘记提到错误的位置,显然是在这一行:

Offset(1, 0).Select

这是有道理的,因为Offset()不是某个函数,它是一个范围的属性。

很可能你想使用你选择的单元格的偏移量,你可以写为:

Selection.Offset(1, 0).Select

祝好运

最新更新