Edit Macro - Filter on Arrays When Header not in Row 1 (chart + acro included)

klopera1

New Member
Joined
Apr 14, 2014
Messages
10
I have the following macro, with altered arrays for simplicity.

It works perfectly in filtering the data I need (in this example, it would keep the columns with Header A, B, or D.

However, I added a graphic to the top g my excel, causing my headers to be in a row different than row 1


How would I got about altering this macro for it to function as it did, with the column headers in a different row placement?

Thank You!




Public Sub Macro1()
Dim iLastCol As Long
Dim iPtr As Long
Dim MyHeaders As Variant, xMatch As Variant


MyHeaders = Array("A", "B", "D")

iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For iPtr = iLastCol To 1 Step -1
xMatch = Application.Match(Cells(1, iPtr).Value, MyHeaders, 0)
If IsError(xMatch) Then
Columns(iPtr).Delete Shift:=xlToLeft
End If
Next iPtr

[TABLE="class: grid, width: 350"]
<TBODY>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]5[/TD]
[TD]5[/TD]
[TD]5[/TD]
[/TR]
</TBODY>[/TABLE]
 
Change the red 1's to your row number


Code:
Public Sub Macro1()
Dim iLastCol As Long
Dim iPtr As Long
Dim MyHeaders As Variant, xMatch As Variant

MyHeaders = Array("A", "B", "D")
iLastCol = Cells([COLOR="#FF0000"]1[/COLOR], Columns.Count).End(xlToLeft).Column
For iPtr = iLastCol To 1 Step -1
xMatch = Application.Match(Cells([COLOR="#FF0000"]1[/COLOR], iPtr).Value, MyHeaders, 0)
If IsError(xMatch) Then
Columns(iPtr).Delete Shift:=xlToLeft
End If
Next iPtr
 
Upvote 0

Forum statistics

Threads
1,226,860
Messages
6,193,398
Members
453,794
Latest member
slilesy

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