radsok8199
New Member
- Joined
- Dec 4, 2020
- Messages
- 24
- Office Version
- 2016
- Platform
- Windows
- MacOS
- Mobile
Dear VBA Masters. Below u will find code I am trying to run but I got lost. I do write this as begginer using tutorials but can not find solution of what i am doing wrong.
I do have 2 similar tables placed on separate worksheets of same workbook. First sheet "Jobs" contains requirements and another "Extract" contains table from DB Extract. Both tables almost same with Headers matching each other. However table contains some extra columns i want to be removed. Amount of columns in both sheets are flexible so range need to be dynamic.
So to summarize: I want my code to use headers from "Extract" as Array and check column Headers from sheet "Jobs". If values match ( headers) then keep column if not then remove column from "Jobs"
Headers are placed in first row for both tables and begins from "D1"
I do have 2 similar tables placed on separate worksheets of same workbook. First sheet "Jobs" contains requirements and another "Extract" contains table from DB Extract. Both tables almost same with Headers matching each other. However table contains some extra columns i want to be removed. Amount of columns in both sheets are flexible so range need to be dynamic.
So to summarize: I want my code to use headers from "Extract" as Array and check column Headers from sheet "Jobs". If values match ( headers) then keep column if not then remove column from "Jobs"
Headers are placed in first row for both tables and begins from "D1"
VBA Code:
Sub removeColTest()
Dim Firstcol As Long
Dim Lastcol As Long
Dim Lcol As Long
Dim Head As Variant
Dim Mylist As Variant
Dim CalcMode As Long
Head = Application.Index(Sheets("Extract").Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Value, 1, 0)
Mylist = Array(Head)
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With Worksheets("Jobs")
Firstcol = .UsedRange.Cells(0).Column
Lastcol = .UsedRange.Columns(.UsedRange.Columns.Count).Column
For Lcol = Lastcol To Firstcol Step -1
With .Cells(1, Lcol)
If Not IsNumeric(WorksheetFunction.Match(Cells(1, Lcol), Mylist, 0)) Then Columns(Lcol).EntireColumn.Delete
End With
Next Lcol
End With
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub
Last edited: