2014-11-05 92 views
1

我試圖用字符串'hello'或'hi'來隱藏行。但是我在if語句中出現語法錯誤。但是對我來說,任何想法都是正確的?excel中的錯誤如果或聲明

這裏是我的代碼

Private Sub CommandButton1_Click() 

    Dim BeginRow As Long 
    Dim EndRow As Long 
    Dim ChkCol As Integer 

    BeginRow = 1 'starting row 
    ChkCol = 2 'column you want to check 
    EndRow = Range("B1").CurrentRegion.Rows.Count 'get the total number of rows 

    For RowCnt = BeginRow To EndRow 
     If (OR(Cells(RowCnt, ChkCol).Value = "hello", Cells(RowCnt, ChkCol).Value = "hi")) Then 
      Cells(RowCnt, ChkCol).EntireRow.Hidden = True 'hide rows 
     End If 
    Next RowCnt 

End Sub 
+0

你嘗試我的建議? – 2014-11-05 14:21:17

回答

1

試試這個:

If Cells(RowCnt, ChkCol).Value = "hello" Or Cells(RowCnt, ChkCol).Value = "hi" Then 
    Cells(RowCnt, ChkCol).EntireRow.Hidden = True 'hide rows 
End If 
1

不,不看的權利。 VBA代碼有其自己的語法,在VBA中沒有Or函數,該語言使用邏輯運算符代替。

此:

If (OR(Cells(RowCnt, ChkCol).Value = "hello", Cells(RowCnt, ChkCol).Value = "hi")) Then 

應該這樣寫:

If Cells(RowCnt, ChkCol).Value = "hello" Or Cells(RowCnt, ChkCol).Value = "hi" Then