简体   繁体   中英

VBA OR IF Statement

I want to use the OR operator so that my function says " if the cell doesn't start with 'rw-promo' or 'rw-content' then delete the entire row.

However, the function is not considering 'rw-content' so I am left with the cells the start with 'rw-promo'.

Any help is appreciated!


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

The problem is that the 'Not' operator does not apply to both of your statements. It appears that way as an English statement, but does not execute that way as a VBA conditional. What your posted code is saying is: "If the cell doesn't start with rw-Promo or if the cell starts with rw-content, delete the cell".What you need is that line to read:

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

The same kind of thing trips people up all over any programming language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM