如何在弹出消息框之前运行以前的代码?

时间:2017-10-11 10:41:24

标签: messagebox

您好我有以下excel vba代码。我遇到的问题是代码的一部分开始" rows(x).select"。当我单步执行代码时,在弹出下一个消息框之前选择该行,但是当我运行代码时,弹出消息框后会选择该行。在弹出消息框之前,我有什么办法可以选择行吗?

Sub deletedata()
Dim x As String, y As Integer, z As Integer
y = Range("auditstart").Row
z = Range("auditend").Offset(-3, 0).Row
x = InputBox("Please enter the row number wich you wish to delete", "DELETE DATA")
If Not IsNumeric(x) Then
MsgBox "please enter a valid number"
Exit Sub
ElseIf CLng(x) < y Or CLng(x) > z Then
MsgBox "Please enter a row number between " & y & " and " & z
Exit Sub
End If

Rows(x).Select

If MsgBox("Are you sure you want to delete row " & x & " ?", vbYesNo) = vbNo Then

Exit Sub
Else
Cells(x, 3).Select
Worksheets("audit").Unprotect password
ActiveCell = "DELETE"
ActiveCell.Offset(0, 3) = 0
ActiveCell.Offset(0, 4) = 0
ActiveCell.Offset(0, 5) = 0
ActiveCell.Offset(0, 6) = 0
ActiveCell.Offset(0, 7) = Range("counter").Value + 1
Sheets("colours").Unprotect password
Range("counter").Value = ActiveCell.Offset(0, 7).Value
Sheets("colours").Protect password
Worksheets("audit").Protect password
End If

End Sub

1 个答案:

答案 0 :(得分:0)

将CLong与CLong进行比较以获取此行:

ElseIf CLng(x) < CLng(y) Or CLng(x) > CLng(z)

然后订单似乎没问题。我把密码放在“”中,因为我认为它是一个被传递的字符串。不是名为密码的变量。

Option Explicit
Sub deletedata()
    Dim x As String, y As Integer, z As Integer
    y = Range("auditstart").Row
    z = Range("auditend").Offset(-3, 0).Row
    x = InputBox("Please enter the row number which you wish to delete", "DELETE DATA")

    If Not IsNumeric(x) Then
        MsgBox "please enter a valid number"
        Exit Sub
    ElseIf CLng(x) < CLng(y) Or CLng(x) > CLng(z) Then
        MsgBox "Please enter a row number between " & y & " and " & z
        Exit Sub
    End If

    Rows(x).Select

    If MsgBox("Are you sure you want to delete row " & x & " ?", vbYesNo) = vbNo Then
        Exit Sub
    Else
        Cells(x, 3).Select
        Worksheets("audit").Unprotect "Password"
        ActiveCell = "DELETE"
        ActiveCell.Offset(0, 3) = 0
        ActiveCell.Offset(0, 4) = 0
        ActiveCell.Offset(0, 5) = 0
        ActiveCell.Offset(0, 6) = 0
        ActiveCell.Offset(0, 7) = Range("counter").Value + 1
        Sheets("colours").Unprotect "Password"
        Range("counter").Value = ActiveCell.Offset(0, 7).Value
        Sheets("colours").Protect "Password"
        Worksheets("audit").Protect "Password"
    End If

End Sub