2014-07-12 2 views
0

열을 반복하는 코드를 작성하려고합니다. 셀에 "buy"또는 "sel"이외의 값이 포함되어 있으면 경고를 표시합니다. 확인에서 하위 메뉴를 종료하고 취소를 계속해야하지만 모든 테스트 셀에 "구매"또는 "판매"가 있으며 메시지 상자가 계속 나타나기 때문에 구매 및 판매 값을 찾지 못합니다. 제발 조언.메시지 상자가 IF 문에서 작동하지 않습니다.

Set TransSheet = Application.ActiveSheet 
LastCell = TransSheet.Cells(Rows.Count, "A").End(xlUp).Row 

Dim MsgBoxTrans As Long 
For a = LastCell To 2 
    v = Cells(a, 10).Value 
    If v <> "buy" Or v <> "sel" Then 
     MsgBoxTrans = MsgBox("Transaction exists other than BUY or SEL, please filter and check", vbOKCancel, "Warning - Press Cancel to Skip") 
     If MsgBoxTrans = vbOK Then 
      Exit Sub 
     End If 
    Else 
     End If 
    a = a - 1 
Next a 
+4

'v <> "buy"또는 v <> "sel" "적어도 하나의 조건이 항상 true 일 것입니다. "구매"가되지 않거나 "판매"되지 않을 것입니다. 'v <> "buy"로 변경하십시오. v <> "sel"' –

답변

0

코드에서 논리적 인 문제를 설명했습니다. 여기에 몇 가지 추가 개선 사항을 추가 할 수 있습니다 :

Set TransSheet = Application.ActiveSheet 
LastCell = TransSheet.Cells(Rows.Count, "A").End(xlUp).Row 
'This variable not really needed: 
'Dim MsgBoxTrans As Long 

'## Declare your variable "v" as Variant 
Dim v as Variant 

'## Force "Step -1" rather than using the clunky "a = a-1": 

For a = LastCell To 2 Step -1 
    v = Cells(a, 10).Value 
    If v <> "buy" AND v <> "sel" Then 
     '## Evaluate the MsgBox object, assign the button type to "vbOkCancel" so that 
     ' it will either be OK (exit sub) or Cancel, which will continue uninterupted 
     If MsgBox("Transaction exists other than BUY or SEL, please filter and check", _ 
        "Warning - Press Cancel to Skip", vbOkCancel) = vbOK Then Exit Sub 
    'Get rid of this "Else" because you're not using it: 
    'Else 
    End If 
Next a 
+0

도움 주셔서 감사합니다! – user3822304

관련 문제