Macro to copy two sheets and delete columns based on headers

thelad

Board Regular
Joined
Jan 28, 2011
Messages
245
Hi,

The following macro is working for me. I'm just wondering would anyone write this differently?

Currently it uses 5 for loops to search the copied sheet for 5 different headers that will be on the sheet multiple times and deletes allcolumns thatcontain these headers.

Its working but just seeing if anyone has a better way or to make faster:

Your help is appreciated:

Sub CommandButton1_Click()
Dim OutlookApp As Object
Dim MItem As Object
Dim Wb As Workbook
Dim NewWb As Workbook
Dim lastCol As Long
Dim delCol As Long

Application.ScreenUpdating = False

'Create Excel sheet link
Set Wb = ActiveWorkbook

Sheets(Array(Sheet2.Name, Sheet3.Name)).Copy
Set NewWb = ActiveWorkbook

astCol = Cells(1, Columns.Count).End(xlToLeft).Column
For delCol = lastCol To 1 Step -1
If Cells(1, delCol) = "ID Number" Then _
Cells(1, delCol).EntireColumn.Delete
Next

lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For delCol = lastCol To 1 Step -1
If Cells(1, delCol) = "Par" Then _
Cells(1, delCol).EntireColumn.Delete
Next

lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For delCol = lastCol To 1 Step -1
If Cells(1, delCol) = "Transaction Code" Then _
Cells(1, delCol).EntireColumn.Delete
Next

lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For delCol = lastCol To 1 Step -1
If Cells(1, delCol) = "Identifier" Then _
Cells(1, delCol).EntireColumn.Delete
Next

lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For delCol = lastCol To 1 Step -1
If Cells(1, delCol) = "Date" Then _
Cells(1, delCol).EntireColumn.Delete
Next

Application.ScreenUpdating = True


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
See if you can implement this bit of code for your 5 loops:
Code:
Sub test()
    Dim lastCol As Long
    Dim delCol As Long
    Dim myCols
 
    myCols = Array("ID Number", "Par", "Identifier", "Date")
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
 
    For i = 0 To UBound(myCols)
        For delCol = lastCol To 1 Step -1
            If Cells(1, delCol) = myCols(i) Then
                Cells(1, delCol).EntireColumn.Delete
            End If
       Next
    Next i
End Sub

Fill in you Array as required. I just put a few in for testing.
 
Upvote 0
Hi,

Thanks I tried it and worked perfectly as before. Thank you again. I am working on something further on this macro but having a problem. If I post it could you have a look if you get a chance please?

I'm going to give it another day and see if can figure it out but if can't be grateful if you could have a look.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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