Excel Vba -编译错误-未定义变量

mctunoxg  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(284)

我很难在工作表中使用两个代码来实现类似的功能,当尝试使用这两个功能时,出现以下消息:未定义变量。
你能帮我把下面的两个代码配起来吗?
代码1:

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    
    Dim celula As Range
    Dim intervalo As Range
    
 
    Set intervalo = Range("B9:B12; B15:B18; B20:B23; B25; B27:B28; B33; B36:B42; B52; A91; B91; C91; D91")


    For Each celula In intervalo
    
        If IsEmpty(celula.Value) Then
            MsgBox "A célula " & celula.Address & " está vazia, favor preencher."
            celula.Interior.Color = vbRed 'adicionar uma cor na celula
            Cancel = True
            
        Else
            celula.Interior.Color = xlNone
            
            
        End If
    
    Next
    

End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim celula As Range
    Dim intervalo As Range
    
 
    Set intervalo = Range("B9:B12; B15:B18; B20:B23; B25; B27:B28; B33; B36:B42; B52; A91:A94; B91:B94; C91:C94; D91;D94")


    For Each celula In intervalo
    
        If IsEmpty(celula.Value) Then
            MsgBox "A célula " & celula.Address & " está vazia, favor preencher."
            celula.Interior.Color = vbRed 'adicionar uma cor na celula
            Cancel = True
            
        Else
            celula.Interior.Color = xlNone
        End If
    
    Next

End Sub

代码2:

Private Sub Workbook_BeforePrint(Cancel As Boolean)

qtdvazias = WorksheetFunction.CountBlank(Range("B9:B11")) + WorksheetFunction.CountBlank(Range("B15:B18"))

If qtdvazias >= 1 Then

MsgBox ("Campos obrigatórios vazios. Favor preencher")
Cancel = True
End If

End Sub

使用代码1是为了使未填写上述单元格的人无法保存或关闭文件,并显示缺失的单元格。
使用代码2,以便在所述单元格为空时不打印文件。
我相信会有一些代码来匹配这两种情况。但我是新的VBA。

m4pnthwp

m4pnthwp1#

在代码2中,qtdvaziasias未定义,因此出现错误消息。您需要从以下内容开始:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim qtdvaziasias as Integer

对于重复的代码,您可以将其提取到单独的Function中,如下所示:

Private Function RequiredCellsMissing() As Boolean

Dim missing As Boolean
    
    missing = False

    Dim celula As Range
    Dim intervalo As Range
    
    Set intervalo = Range("B9:B12; B15:B18; B20:B23; B25; B27:B28; B33; B36:B42; B52; A91:A94; B91:B94; C91:C94; D91; D94")

    For Each celula In intervalo
    
        If IsEmpty(celula.Value) Then
            MsgBox "A célula " & celula.Address & " está vazia, favor preencher."
            celula.Interior.Color = vbRed 'adicionar uma cor na celula
            missing = True
        Else
            celula.Interior.Color = xlNone
        End If
    
    Next

    RequiredCellsMissing = missing

End Function

然后,只需修改现有代码即可:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Cancel = RequiredCellsMissing()

End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Cancel = RequiredCellsMissing()

End Sub
kq0g1dla

kq0g1dla2#

当我将您的代码粘贴到工作簿中时,我没有得到“未定义变量”。但在您使用过的任何地方,我确实得到了“应用程序定义或对象定义的错误”

Set intervalo = Range("B9:B12; B15:B18; B20:B23; B25; B27:B28; B33; B36:B42; B52; A91:A94; B91:B94; C91:C94; D91;D94")

这是因为这样的范围分隔符不是分号,而是逗号。

Range("B9:B12,B15:B18,B20:B23,B25,B27:B28,B33,B36:B42,B52,A91:A94,B91:B94,C91:C94,D91,D94")

相关问题