如何显示错误消息并停止宏

1 excel vba

我是VBA编码的新手。我想添加一个弹出框,显示消息“今天未找到今天”,如果在K列中未找到今天的日期,则停止宏。不确定如何执行此操作以及代码应在哪里。

' Find the last row on the count sheet and the next row on the archieve sheet

lastRow = countSheet.Cells(countSheet.Rows.count, "K").End(xlUp).Row
nextRow = archieveSheet.Cells(countSheet.Rows.count, "K").End(xlUp).Row + 1

' Look at all rows in the count sheet
For thisRow = 1 To lastRow

    ' Check if column K contains today's date
    If countSheet.Cells(thisRow, "K").Value = Date Then

        ' Copy the entire row to the archieve sheet
        countSheet.Cells(thisRow, "K").EntireRow.Copy Destination:=archieveSheet.Cells(nextRow, "A")

        ' Move to the next row on the archieve sheet
        nextRow = nextRow + 1
    End If
Next thisRow
Run Code Online (Sandbox Code Playgroud)

Chr*_*007 5

在开始时添加一些简单的代码将使所有事情保持简单

If Application.WorksheetFunction.CountIf(countsheet.Range("K:K"), Date) = 0 Then
    MsgBox "Today's Date Not Found"
    Exit Sub
End If
Run Code Online (Sandbox Code Playgroud)

  • 这个!守卫条款FTW!尽早失败,快速摆脱困境-减少嵌套并保持“快乐之路”专注于完成任务。:+1: (3认同)