Adding/removing columns to match a value

bahllr

Board Regular
Joined
May 7, 2009
Messages
62
I am trying to write code in vba that will add/remove columns in a spreadsheet depending on the value of a cell. If the cell (B5) is 4, the code will add/remove columns until there are only 4 columns (starting on column BQ.

It seems I am only spinning my wheels....please help! :)
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
To remove columns

Code:
Sub RemCols()
Dim LC As Integer
LC = ActiveSheet.UsedRange.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Range(Cells(1, Range("B5").Value + 1), Cells(1, LC)).EntireColumn.Delete
End Sub
 
Upvote 0
That is correct. If column AX equals something other than Active or Operational the row should be removed
 
Upvote 0
VOG,

Thanks for your speedy reply. I think I was a bit unclear in my initial explanation. Rather than add/remove..I would like to hide/unhide.

Example: If cell B5 has the value of 5

I would like columns BQ:BU to show/unhide. If the value in B5 is 6, I want columns BQ:BV to show. I want the default to be BQ:CC to be hidden.

Does this make sense?

Thanks.
 
Upvote 0
That is correct. If column AX equals something other than Active or Operational the row should be removed

Try

Code:
Sub Del()
Dim LR As Long, i As Long
LR = Range("AX" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    With Range("AX" & i)
        If .Value <> "Active" And .Value <> "Operational" Then Rows(i).Delete
    End With
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,797
Messages
6,121,629
Members
449,041
Latest member
Postman24

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