Use If Statement to Delete Entire Column Based on Specific Text of a Cell

jewkes6000

Board Regular
Joined
Mar 25, 2020
Messages
60
Office Version
  1. 365
Platform
  1. Windows
I am trying to cycle through several columns and delete the columns which have the word "HIDE" in a specific cell. I'm able to cycle through the cells; however, whenever a cell contains the word "Hide", it skips it as if the statement is NOT true. The cell does contain a formula which generates the cell value, but I though the code should still recognize the value. Why is the if statement "skipping" to the next column when the cell actually says "Hide"? I've also included a google drive link below to see the actual file.

Any help is much appreciated.


VBA Code:
Sub Delete_Columns()

'This will cycle through columns and delete those which are marked as "hide"
    For i = 22 To 237
        Columns(i).Select
        
        If ActiveCell = "Hide" Then
        Columns(i).Delete
        Else
        ActiveCell.Offset(1, 0).Range("A1").Select
        End If

        If ActiveCell = "Hide" Then
        Columns(i).Delete
        Else
        End If
    
    Next i
      
End Sub


 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Perhaps case is the issue.

Try changing these references:
VBA Code:
ActiveCell = "Hide"
to this:
VBA Code:
UCase(ActiveCell) = "HIDE"

By the way, which row are you checking? We can simplify, improve and speed up your code.
If the words always appear in row 1, then you could re-write your code:
VBA Code:
Sub Delete_Columns()

    Dim i As Long
    
    Application.ScreenUpdating = False

    For i = 237 To 22 Step -1
        If UCase(Cells(1, i)) = "HIDE" Then Columns(i).Delete
    Next i
    
    Application.ScreenUpdating = True
      
End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,665
Members
449,114
Latest member
aides

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