VBA或IF语句

时间:2019-02-11 21:10:42

标签: excel vba

我想使用OR运算符,以便我的函数说“如果单元格不是以'rw-promo'或'rw-content'开头,则删除整行。

但是,该函数未考虑“ rw-content”,因此我将以“ rw-promo”开头的单元格保留下来。

感谢您的帮助!


Sub monthly_delete_emails()

    'Disab;e certain Excel features, whilst the macro is running'
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    Application.ScreenUpdating = False

        'Declare variables'
        Dim deleteRow As Long
        Dim ws As Worksheet

        'Set objects'
        Set ws = ActiveSheet

         'Loop through the rows of data, in order to delete rows with'
         'a value in column B. Our macro starts at row 9'
         For deleteRow = ws.Range("B" & Rows.Count).End(xlUp).Row To 9 Step -1

             'identify values in col B, which start with ___'
             If Not ws.Range("B" & deleteRow).Value Like "rw-promo*" Or ws.Range("B" & deleteRow).Value Like "rw-content*" Then
                 Rows(deleteRow).EntireRow.delete
             End If

         'Move to next cell in the range, which is being looped'
         Next deleteRow

    'Re-enable the above Excel features, which were disables whilst the macro ran'
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

问题在于“非”运算符不适用于您的两个语句。它以英语语句的形式出现,但没有以VBA条件的形式执行。您发布的代码所说的是:“如果该单元格不以rw-Promo开头,或者如果该单元格以rw-content开头,则删除该单元格。”您需要的是该行以读取:

If Not ws.Range("B" & deleteRow).Value Like "rw-promo*" Or Not ws.Range("B" & deleteRow).Value Like "rw-content*" Then

同样的事情使人们陷入了任何编程语言的困境。