错误在Excel中如果或声明

我试图隐藏string'你好'或'嗨'的行。 但是我得到一个语句错误的if语句。 但是对我来说,任何想法都是正确的?

这是我的代码

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 

尝试这个:

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

不,不正确。 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