如何让VBA(excel)告诉我代码停止的单元格

时间:2016-01-12 13:49:13

标签: excel vba excel-vba

我希望这是可能的。我想知道如何让excel告诉我它遇到了什么单元格我预定义的“错误”。例如,部分代码是:

Sub CheckErrors()

For Each Cel In Range("A3:A400")

    If Cel.Value = "CR" Then
        If IsEmpty(Cel.Offset(0, 6)) = True Then
        Msgbox "Add a description (name) when creating"
        End If
    End If

    If Cel.Value = "CR" Then
        If IsEmpty(Cel.Offset(0, 7)) = True Then
        Msgbox "Please choose type when creating"
        Exit For
        End If
    End If
Next

我希望消息框还包含哪些特定的单元格excel发现为空。因此,如果在列A中存在CR,则列G必须具有描述,并且我希望消息框在创建时添加描述(名称),如果G3为空而A3中具有CR则修改单元格G3。

感谢任何帮助。我是VBA和编码的新手,所以即使是最基本的也可能有用!

此致 吉姆

1 个答案:

答案 0 :(得分:2)

好吧,你可以在MSGBOX财产.address内添加......这样:

Sub CheckErrors()

For Each Cel In Range("A3:A400")

    If Cel.Value = "CR" Then
        If IsEmpty(Cel.Offset(0, 6)) = True Then
        Msgbox "Add a description (name) when creating " & Cel.Offset(0, 6).address
        End If
    End If

    If Cel.Value = "CR" Then
        If IsEmpty(Cel.Offset(0, 7)) = True Then
        Msgbox "Please choose type when creating " & Cel.Offset(0, 7).address
        Exit For
        End If
    End If
Next

End Sub

您的问题含糊不清,请提供更多关于您真正想要的信息,您为实现这一目标所做的工作,您遇到的错误或任何其他结果......

修改

Nick Dewitt 的评论中,您会看到需要替换地址$中的Replace(Cel.Offset(0, 6).address, "$", "")

编辑#2

Sub CheckErrors()

For Each Cel In Range("A3:A400")

    If Cel.Value = "CR" Then
        If IsEmpty(Cel.Offset(0, 6)) = True Then
        Msgbox "Add a description (name) when creating " & Replace(Cel.Offset(0, 6).address, "$", "")
        End If
    End If

    If Cel.Value = "CR" Then
        If IsEmpty(Cel.Offset(0, 7)) = True Then
        Msgbox "Please choose type when creating " & Replace(Cel.Offset(0, 6).address, "$", "")
        Exit For
        End If
    End If
Next

End Sub