繁体   English   中英

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

[英]How do I make VBA (excel) tell me the cell where my code stopped

我希望这是可能的。 我想知道如何使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列中必须有描述,并且如果A3中有CR,而G3为空,我希望消息框说“创建时添加描述(名称),修改单元格G3”。

任何帮助表示赞赏。 我对VBA和编码非常陌生,因此即使最基本的内容也可能会有所帮助!

问候吉姆

好吧,您可以在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM