2017-10-11 84 views
0

嗨,我有以下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 
+0

這是什麼? Visual Basic? – mumpitz

回答

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 
+0

代碼工作正常,不需要編輯。問題是,從行的消息框:如果MsgBox(「您確定要刪除行」&x&「?」,vbYesNo)= vbNo然後 Exit Sub –

+0

在執行行行(x)之前運行.select –

+0

我希望用戶在消息框出現時看到選中的行 –

相關問題