Delete column change counter

bobkap

Active Member
Joined
Nov 22, 2009
Messages
323
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
This seems ridiculously easy, but I cannot figure it out after many tries.

Scenario: I have approx 35 columns (and rows of course) of data. I want to sequence through each column and delete any columns that meet a certain condition. Here's one of my many attempts that do not work. Example of why it doesn't work: Say column 3 meets the condition and gets deleted. What was col #4 now becomes #3 but I've gone past #3 now so it does not test col #3 for the condition. Somehow, I need to reset the counter so that it address this. Any help would be greatly appreciated.

counter = 1
For counter = counter To finalcol
If Cells(1, counter) <> "Phone" Then
Columns(counter).Delete
counter = counter - 1
End If
Next counter
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
When deleting columns/rows you should always work backwards. Try
Code:
For counter = finalcol To 1 Step -1
    If Cells(1, counter) <> "Phone" Then
        Columns(counter).Delete
    End If
Next counter
 
Upvote 0
AMAZING!! Mega thanks. I might have thought of myself maybe in about 20 years!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,749
Messages
6,174,275
Members
452,553
Latest member
red83

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