excel if或statement中的错误

时间:2014-11-05 03:56:40

标签: excel vba

我试图用'hello'或'hi'字符串隐藏行。但我在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

2 个答案:

答案 0 :(得分: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 :(得分: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