Add formula to last used column, fill down, delete columns based on named range

Aviles

Board Regular
Joined
Dec 17, 2008
Messages
163
Hi all,

I found the below code which adds a column header next to the last used column:

VBA Code:
Dim colLast As Long

    colLast = Cells(1, Columns.Count).End(xlToLeft).Column
    Cells(1, colLast + 1) = "New Column Header"

What I would like to do is to add a formula below this new column header (new column, row 2) and then fill down according to the last row in column A (maybe using Rowcount?)

Then I would like to organise the columns by sorting them in a specific order and deleting columns that are not necessary. Seeing as there are around 30 columns and I only need to see 15, would it be possible to make a list of required headers by using a named range with a specific order and have the code delete those columns that are not on that named range? The column names will always be the same.

I found this code by @Fluff but I could not make it work.

VBA to delete entire column based on column header name

VBA Code:
Sub Nirvehex()
   Dim Ary As Variant
   Dim i As Long
   Dim Fnd As Range
   
   With Sheets("List")
      Ary = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Value2
   End With
   For i = 0 To UBound(Ary)
      Set Fnd = Range("1:1").Find(Ary(i, 1), , xlValues, xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then Fnd.EntireColumn.Delete
   Next i
End Sub


Any help would be appreciated. Thanks in advance.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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