2017-10-11 3 views
0

안녕하세요, 저는 아래 VB 코드를 가지고 있습니다. 문제는 "행 (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) 

주문 후 확인 될 것으로 보인다. 패스워드를 전달하는 문자열이라고 가정 할 때 Password를 ""입력했습니다. 암호라는 변수가 아닙니다.

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 ("행을 삭제 하시겠습니까?", vbYesNo) = vbNo Then Exit Sub –

+0

은 행 행 (x)을 실행하기 전에 실행됩니다. .select –

+0

메시지 상자가 나타날 때 선택한 줄을 사용자가 보길 원합니다. –

관련 문제