VBA Code to autofilter data using column name

aman08u

New Member
Joined
Jun 2, 2021
Messages
5
Office Version
  1. 2016
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
Hi,

I am trying to use below code to autofilter the data in an Excel sheet. But, I have 2 columns with same name i-e 'Labels' in an excel.
When I use below code, it always refer the first occurrence of the 'Label' column and ignore the other one.
Can anyone help me here please? I am new to VBA so not able to figure out the correct way.

Many thanks in advance!!!!!


Dim p As Integer, rngData7 As Range
Set rngData7 = Range("A1").CurrentRegion
p = Application.WorksheetFunction.Match("Labels", Range("A1:ZA1"), 0)
rngData7.AutoFilter p, Array("IMP", "BLB", "CTP", "KTLO", "REG", "MaintenanceRR", "OnTopOf", "Prior", "T1", "T2", "="), xlFilterValues

Dim p1 As Integer, rngData77 As Range
Set rngData77 = Range("A1").CurrentRegion
p1 = Application.WorksheetFunction.Match("Labels", Range("A1:ZA1"), 0)
rngData77.AutoFilter p1, Array("IMP", "BLB", "CTP", "KTLO", "REG", "MaintenanceRR", "OnTopOf", "Prior", "T1", "T2", "="), xlFilterValues
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this.

The first search is made towards the next, the second search is made to the previous one

VBA Code:
Sub FilterColumns()
  Dim p As Integer
  Dim rngData7 As Range
  Dim f As Range
  Dim ary As Variant
  
  ary = Array("IMP", "BLB", "CTP", "KTLO", "REG", "MaintenanceRR", "OnTopOf", "Prior", "T1", "T2", "=")
  Set rngData7 = Range("A1").CurrentRegion
  Set f = Range("A1:ZA1").Find("Labels", , xlValues, xlWhole, , xlNext, False)
  If Not f Is Nothing Then
    p = f.Column
    rngData7.AutoFilter p, Array(ary), xlFilterValues
  End If
  
  Set f = Range("A1:ZA1").Find("Labels", , xlValues, xlWhole, , xlPrevious, False)
  If Not f Is Nothing Then
    p = f.Column
    rngData7.AutoFilter p, Array(ary), xlFilterValues
  End If
End Sub
 
Upvote 0
Thanks @DanteAmor for the quick turnaround. This is working fine for 2 columns with same name. But suppose if I have 3 columns with same name, then what needs to change here.
Try this.

The first search is made towards the next, the second search is made to the previous one

VBA Code:
Sub FilterColumns()
  Dim p As Integer
  Dim rngData7 As Range
  Dim f As Range
  Dim ary As Variant
 
  ary = Array("IMP", "BLB", "CTP", "KTLO", "REG", "MaintenanceRR", "OnTopOf", "Prior", "T1", "T2", "=")
  Set rngData7 = Range("A1").CurrentRegion
  Set f = Range("A1:ZA1").Find("Labels", , xlValues, xlWhole, , xlNext, False)
  If Not f Is Nothing Then
    p = f.Column
    rngData7.AutoFilter p, Array(ary), xlFilterValues
  End If
 
  Set f = Range("A1:ZA1").Find("Labels", , xlValues, xlWhole, , xlPrevious, False)
  If Not f Is Nothing Then
    p = f.Column
    rngData7.AutoFilter p, Array(ary), xlFilterValues
  End If
End Sub
 
Upvote 0
I have 2 columns with same name
That was not in your initial question. It would be necessary to make another design to find more than 2 columns.
You really have a scenario with more than 2 columns. It would be more convenient if each column has its own name "Labels1", "Labels2", "Labels3" ... Even for a pivot table, for example, you need different names.
 
Upvote 0
That was not in your initial question. It would be necessary to make another design to find more than 2 columns.
You really have a scenario with more than 2 columns. It would be more convenient if each column has its own name "Labels1", "Labels2", "Labels3" ... Even for a pivot table, for example, you need different names.
Actually I am fetching data from JIRA. So in my jira project we have one text box called Labels. If Labels field have 2 or 3 values, then while downloading the data into excel...it will automatically creates 2 or 3 Labels column.
 
Upvote 0
For multiple columns, try this:

VBA Code:
Sub FilterColumns()
  Dim rngData7 As Range, f As Range
  Dim ary As Variant
  Dim cell As String
  
  ary = Array("IMP", "BLB", "CTP", "KTLO", "REG", "MaintenanceRR", "OnTopOf", "Prior", "T1", "T2", "=")
  Set rngData7 = Range("A1").CurrentRegion
  Set f = rngData7.Rows(1).Find("Labels", , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      rngData7.AutoFilter f.Column, Array(ary), xlFilterValues
      Set f = rngData7.Rows(1).FindNext(f)
    Loop While Not f Is Nothing And f.Address <> cell
  End If
End Sub
 
Upvote 0
For multiple columns, try this:

VBA Code:
Sub FilterColumns()
  Dim rngData7 As Range, f As Range
  Dim ary As Variant
  Dim cell As String
 
  ary = Array("IMP", "BLB", "CTP", "KTLO", "REG", "MaintenanceRR", "OnTopOf", "Prior", "T1", "T2", "=")
  Set rngData7 = Range("A1").CurrentRegion
  Set f = rngData7.Rows(1).Find("Labels", , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      rngData7.AutoFilter f.Column, Array(ary), xlFilterValues
      Set f = rngData7.Rows(1).FindNext(f)
    Loop While Not f Is Nothing And f.Address <> cell
  End If
End Sub
Cheers!!! Thank you so much. This is exactly what I was looking for. Appreciate your help :)
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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