IF w/Not Equal?

bobkap

Active Member
Joined
Nov 22, 2009
Messages
313
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I am trying to delete columns if the header of the column is NOT a header containing the word "Transaction Status" and other headers, but when I run this (below) it doesn't delete columns based on these criteria. It deletes every column.

Any help on what I'm doing wrong would be greatly appreciated.

If Cells(1, counter) <> "Transaction Status" Or Cells(1, counter) <> "Settlement Amount" Or Cells(1, counter) <> "Settlement Date/Time" Or Cells(1, counter) <> "Submit Date/Time" Or Cells(1, counter) <> "Date" Then
Columns(counter).Delete
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If by the "containing" in "containing the word" you mean the word might not be the only text in the cell then you will also need to use another method, one way is something like...

Code:
Sub DelCols()
    Dim counter As Long
    For counter = 10 To 1 Step -1
        If InStr(Cells(1, counter), "Transaction Status") = 0 And _
          InStr(Cells(1, counter), "Settlement Amount") = 0 And _
            InStr(Cells(1, counter), "Settlement Date/Time") = 0 And _
              InStr(Cells(1, counter), "Submit Date/Time") = 0 And _
                InStr(Cells(1, counter), "Date") = 0 Then Columns(counter).Delete
    Next
End Sub

Although if this is the case you can do away with 2 of the tests as you are testing for "Date".
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,178
Members
448,871
Latest member
hengshankouniuniu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top