Macro to always select the first option available in filtered column

thachad098

New Member
Joined
Aug 11, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone, I hit a roadblock when I created a macro and I am hoping someone with extensive macro/VBA experience would be able to help me out.

What I am trying to do is to add a filter in column A, select the first option, do a bunch of processing on the data, copy it to a FINAL sheet and delete that data so the top-most option on the filter will be the next item to be processed.

The challenge that I am experiencing is on the filter portion. Instead of recording checkbox1, its recording the actual column value selected. For simplicity, I have created a simple macro of this filter/delete process. This has column A named TEST with values 1,2,3,4,5. As you can see, the criteria reflects the value that I selected on the filter.

Sub FILTER_AND_DELETE()
'
' FILTER_AND_DELETE Macro
'

'
Columns("A:A").Select
Selection.AutoFilter
Range("A2").Select
ActiveSheet.Range("$A$1:$A$6").AutoFilter Field:=1, Criteria1:="1"
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.AutoFilter
Columns("A:A").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$6").AutoFilter Field:=1, Criteria1:="2"
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.AutoFilter
Columns("A:A").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$6").AutoFilter Field:=1, Criteria1:="3"
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.AutoFilter
Columns("A:A").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$6").AutoFilter Field:=1, Criteria1:="4"
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.AutoFilter
Columns("A:A").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$6").AutoFilter Field:=1, Criteria1:="<>"
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Columns("A:A").Select
Selection.AutoFilter
Columns("A:A").Select
Selection.AutoFilter
End Sub

Any help that you can provide on this is highly appreciated.

Chad
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi Chad,

test for code was made on this sample:
MrE macro-to-always-select-the-first-option-available-in-filtered-column.1213315 220811.xlsm
AB
1NumbersOrder
21alpha
32beta
43charlie
54delta
65echo
76foxtrott
Data


In order to make SpecialCells work as expected it´s necessary that at least 2 cells should be visible when filtered.

Code used based on an old thread from this forum:
Code:
Sub Chad()
'https://www.mrexcel.com/board/threads/macro-to-always-select-the-first-option-available-in-filtered-column.1213315/
'code based on https://www.mrexcel.com/board/threads/vba-to-advance-to-next-unique-criteria-in-autofilter-list.1081461/post-5195923
'thanks Fluff
 
  Dim rngCell As Range
  Dim lngCounter As Long
  Dim objDic As Object
 
  Const cstrSheetData As String = "Data"        'change to suit
  Const cstrSheetTarget As String = "Collect"   'change to suit
 
  With Sheets(cstrSheetData)
    If .AutoFilterMode Then .Range("A1").AutoFilter
    Set objDic = CreateObject("scripting.dictionary")
    For Each rngCell In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
      objDic.Item(rngCell.Value) = Empty
    Next rngCell
    For lngCounter = 0 To objDic.Count - 1
      .Range("A1").CurrentRegion.AutoFilter 1, objDic.Keys()(lngCounter)
        With .Range("A2", .Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
          .Copy Sheets("Collect").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
          .EntireRow.Delete
        End With
    Next lngCounter
    If .AutoFilterMode Then .Range("A1").AutoFilter
  End With
End Sub
Please check on a sample file with Sheets Data and Collect or adjust the names of the worksheets to suit.

Ciao,
Holger
 
Upvote 0
Hi Chad,

test for code was made on this sample:
MrE macro-to-always-select-the-first-option-available-in-filtered-column.1213315 220811.xlsm
AB
1NumbersOrder
21alpha
32beta
43charlie
54delta
65echo
76foxtrott
Data


In order to make SpecialCells work as expected it´s necessary that at least 2 cells should be visible when filtered.

Code used based on an old thread from this forum:
Code:
Sub Chad()
'https://www.mrexcel.com/board/threads/macro-to-always-select-the-first-option-available-in-filtered-column.1213315/
'code based on https://www.mrexcel.com/board/threads/vba-to-advance-to-next-unique-criteria-in-autofilter-list.1081461/post-5195923
'thanks Fluff
 
  Dim rngCell As Range
  Dim lngCounter As Long
  Dim objDic As Object
 
  Const cstrSheetData As String = "Data"        'change to suit
  Const cstrSheetTarget As String = "Collect"   'change to suit
 
  With Sheets(cstrSheetData)
    If .AutoFilterMode Then .Range("A1").AutoFilter
    Set objDic = CreateObject("scripting.dictionary")
    For Each rngCell In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
      objDic.Item(rngCell.Value) = Empty
    Next rngCell
    For lngCounter = 0 To objDic.Count - 1
      .Range("A1").CurrentRegion.AutoFilter 1, objDic.Keys()(lngCounter)
        With .Range("A2", .Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
          .Copy Sheets("Collect").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
          .EntireRow.Delete
        End With
    Next lngCounter
    If .AutoFilterMode Then .Range("A1").AutoFilter
  End With
End Sub
Please check on a sample file with Sheets Data and Collect or adjust the names of the worksheets to suit.

Ciao,
Holger
Hello Holger,

Thank you for taking the time for looking into my inquiry. What you have provided does not meet what I need, but that is on me not providing the whole picture. Kindly see attached image on how the data looks and how it should be transformed. I have 3 sheets; DATA where the initial records will be dumped, STAGING where each record that matches the filter will be processed and COLLECT to consolidate the final output. I have also provided the updated script below of the whole process on how I did it, but for my requirement the Criteria1 should not be fixed value as the records extracted what will use this macro varies.

I also recorded up to 10 lines to anticipate records extracted that exceed my current values.

Sub FILTER_MACRO()
'
' FILTER_MACRO Macro
'

'
Cells.Select
Selection.AutoFilter
Range("A1").Select
ActiveSheet.Range("$A$1:$L$20").AutoFilter Field:=1, Criteria1:="1"
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Staging").Select
Range("A1").Select
ActiveSheet.Paste
Range("B2").Select
Application.CutCopyMode = False
Selection.Cut
Range("C1").Select
ActiveSheet.Paste
Range("B3").Select
Selection.Cut
Range("D1").Select
ActiveSheet.Paste
Range("B4").Select
Selection.Cut
Range("E1").Select
ActiveSheet.Paste
Range("B5").Select
Selection.Cut
Range("F1").Select
ActiveSheet.Paste
Range("B6").Select
Selection.Cut
Range("G1").Select
ActiveSheet.Paste
Range("B7").Select
Selection.Cut
Range("H1").Select
ActiveSheet.Paste
Range("B8").Select
Selection.Cut
Range("I1").Select
ActiveSheet.Paste
Range("B9").Select
Selection.Cut
Range("J1").Select
ActiveSheet.Paste
Range("B10").Select
Selection.Cut
Range("K1").Select
ActiveSheet.Paste
Rows("1:1").Select
Selection.Cut
Sheets("Collect").Select
Rows("2:2").Select
Selection.Insert Shift:=xlDown
Sheets("Staging").Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Sheets("Data").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
Cells.Select
Selection.AutoFilter
Selection.AutoFilter
Range("A1").Select
ActiveSheet.Range("$A$1:$L$20").AutoFilter Field:=1, Criteria1:="2"
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Sheets("Staging").Select
ActiveSheet.Paste
Range("B2").Select
Application.CutCopyMode = False
Selection.Cut
Range("C1").Select
ActiveSheet.Paste
Range("B3").Select
Selection.Cut
Range("D1").Select
ActiveSheet.Paste
Range("B4").Select
Selection.Cut
Range("E1").Select
ActiveSheet.Paste
Range("B5").Select
Selection.Cut
Range("F1").Select
ActiveSheet.Paste
Range("B6").Select
Selection.Cut
Range("G1").Select
ActiveSheet.Paste
Range("B7").Select
Selection.Cut
Range("H1").Select
ActiveSheet.Paste
Range("B8").Select
Selection.Cut
Range("I1").Select
ActiveSheet.Paste
Range("B9").Select
Selection.Cut
Range("J1").Select
ActiveSheet.Paste
Range("B10").Select
Selection.Cut
Range("K1").Select
ActiveSheet.Paste
Rows("1:1").Select
Selection.Cut
Sheets("Collect").Select
Selection.Insert Shift:=xlDown
Sheets("Staging").Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Sheets("Data").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
Cells.Select
Selection.AutoFilter
Selection.AutoFilter
Range("A1").Select
ActiveSheet.Range("$A$1:$L$20").AutoFilter Field:=1, Criteria1:="<>"
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Staging").Select
Range("A1").Select
ActiveSheet.Paste
Range("B2").Select
Application.CutCopyMode = False
Selection.Cut
Range("C1").Select
ActiveSheet.Paste
Range("B3").Select
Selection.Cut
Range("D1").Select
ActiveSheet.Paste
Range("B4").Select
Selection.Cut
Range("E1").Select
ActiveSheet.Paste
Range("B5").Select
Selection.Cut
Range("F1").Select
ActiveSheet.Paste
Range("B6").Select
Selection.Cut
Range("G1").Select
ActiveSheet.Paste
Range("B7").Select
Selection.Cut
Range("H1").Select
ActiveSheet.Paste
Range("B8").Select
Selection.Cut
Range("I1").Select
ActiveSheet.Paste
Range("B9").Select
Selection.Cut
Range("J1").Select
ActiveSheet.Paste
Range("B10").Select
Selection.Cut
Range("K1").Select
ActiveSheet.Paste
Rows("1:1").Select
Selection.Cut
Sheets("Collect").Select
Selection.Insert Shift:=xlDown
Sheets("Staging").Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Sheets("Data").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
Cells.Select
Selection.AutoFilter
Selection.AutoFilter
Range("A1").Select
End Sub

Any input that you can provide on this is highly appreciated.
 

Attachments

  • excel sample.jpg
    excel sample.jpg
    115.9 KB · Views: 8
Upvote 0
Hi Chad,

there are two things I would like to mention: if you post code please wrap it in [ code ]Your code[ /code ] without blanks inside the square brackets or mark the code and hit the VBA symbol at the top of the rply window. This would enable anybody to get the whole code with one single click to the clipboard (with all white spaces from the original code) and limit the space the code takes on viewing the thread/post. Second is please give a description of how the original data looks like and what you want to do. If I get a good descritption of what you want it´s like a plan of what to code for me.

From all I see you want to copy and Paste/Transpose. Based on your picture this is what you have:
MrE macro-to-always-select-the-first-option-available-in-filtered-column.1213315 220811.xlsm
ABCDEFGHIJKLMN
1KEYVal1Val2Val3Val4Val5Val6Val7Val8Val9Val10DATABefore
21qDATABefore
31wDATABefore
41eDATABefore
51rDATABefore
61tDATABefore
71yDATABefore
82aDATABefore
92sDATABefore
102dDATABefore
112fDATABefore
122gDATABefore
132hDATABefore
142jDATABefore
152kDATABefore
163zDATABefore
173xDATABefore
183cDATABefore
19
20KEYVal1Val2Val3Val4Val5Val6Val7Val8Val9Val10STAGINGBefore
Tabelle2


And this is what you want to get:
MrE macro-to-always-select-the-first-option-available-in-filtered-column.1213315 220811.xlsm
ABCDEFGHIJKLMN
1KEYVal1Val2Val3Val4Val5Val6Val7Val8Val9Val10DATAAfter
2
3KEYVal1Val2Val3Val4Val5Val6Val7Val8Val9Val10STAGINGAfter
43zxcSTAGINGAfter
52asdfghjkSTAGINGAfter
61qwertySTAGINGAfter
Tabelle3


Code used:
Code:
Sub Chad_Scenario2_Transpose()
'https://www.mrexcel.com/board/threads/macro-to-always-select-the-first-option-available-in-filtered-column.1213315/
  
  Dim rngCell As Range
  Dim lngCounter As Long
  Dim objDic As Object
  Dim wsData As Worksheet
  Dim wsStaging As Worksheet
  
  Set wsData = ThisWorkbook.Sheets("DATA")
  Set wsStaging = ThisWorkbook.Sheets("STAGING")
  
  With wsData
    If .AutoFilterMode Then .Range("A1").AutoFilter
    Set objDic = CreateObject("scripting.dictionary")
    For Each rngCell In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
      objDic.Item(rngCell.Value) = Empty
    Next rngCell
    For lngCounter = 0 To objDic.Count - 1
      .Range("A1").CurrentRegion.AutoFilter 1, objDic.Keys()(lngCounter)
        With .Range("B2", .Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
          wsStaging.Range("A2").EntireRow.Insert
          .Copy
          wsStaging.Range("B2").PasteSpecial Paste:=xlPasteAll, _
                                              Operation:=xlNone, _
                                              SkipBlanks:=False, _
                                              Transpose:=True
          wsStaging.Range("A2").Value = objDic.Keys()(lngCounter)
          .EntireRow.Delete
        End With
    Next lngCounter
    If .AutoFilterMode Then .Range("A1").AutoFilter
  End With

  Set wsStaging = Nothing
  Set wsData = Nothing

End Sub
I changed the original data to look like
MrE macro-to-always-select-the-first-option-available-in-filtered-column.1213315 220811.xlsm
AB
26KEYVal1
272a
283c
292d
301e
312f
322g
332h
342j
352k
361q
371r
382s
391t
401w
413x
421y
433z
Tabelle1

and got the same result from the code.

Ciao,
Holger
 
Upvote 0
Solution
Hi Chad,

there are two things I would like to mention: if you post code please wrap it in [ code ]Your code[ /code ] without blanks inside the square brackets or mark the code and hit the VBA symbol at the top of the rply window. This would enable anybody to get the whole code with one single click to the clipboard (with all white spaces from the original code) and limit the space the code takes on viewing the thread/post. Second is please give a description of how the original data looks like and what you want to do. If I get a good descritption of what you want it´s like a plan of what to code for me.

From all I see you want to copy and Paste/Transpose. Based on your picture this is what you have:
MrE macro-to-always-select-the-first-option-available-in-filtered-column.1213315 220811.xlsm
ABCDEFGHIJKLMN
1KEYVal1Val2Val3Val4Val5Val6Val7Val8Val9Val10DATABefore
21qDATABefore
31wDATABefore
41eDATABefore
51rDATABefore
61tDATABefore
71yDATABefore
82aDATABefore
92sDATABefore
102dDATABefore
112fDATABefore
122gDATABefore
132hDATABefore
142jDATABefore
152kDATABefore
163zDATABefore
173xDATABefore
183cDATABefore
19
20KEYVal1Val2Val3Val4Val5Val6Val7Val8Val9Val10STAGINGBefore
Tabelle2


And this is what you want to get:
MrE macro-to-always-select-the-first-option-available-in-filtered-column.1213315 220811.xlsm
ABCDEFGHIJKLMN
1KEYVal1Val2Val3Val4Val5Val6Val7Val8Val9Val10DATAAfter
2
3KEYVal1Val2Val3Val4Val5Val6Val7Val8Val9Val10STAGINGAfter
43zxcSTAGINGAfter
52asdfghjkSTAGINGAfter
61qwertySTAGINGAfter
Tabelle3


Code used:
Code:
Sub Chad_Scenario2_Transpose()
'https://www.mrexcel.com/board/threads/macro-to-always-select-the-first-option-available-in-filtered-column.1213315/
 
  Dim rngCell As Range
  Dim lngCounter As Long
  Dim objDic As Object
  Dim wsData As Worksheet
  Dim wsStaging As Worksheet
 
  Set wsData = ThisWorkbook.Sheets("DATA")
  Set wsStaging = ThisWorkbook.Sheets("STAGING")
 
  With wsData
    If .AutoFilterMode Then .Range("A1").AutoFilter
    Set objDic = CreateObject("scripting.dictionary")
    For Each rngCell In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
      objDic.Item(rngCell.Value) = Empty
    Next rngCell
    For lngCounter = 0 To objDic.Count - 1
      .Range("A1").CurrentRegion.AutoFilter 1, objDic.Keys()(lngCounter)
        With .Range("B2", .Range("B" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
          wsStaging.Range("A2").EntireRow.Insert
          .Copy
          wsStaging.Range("B2").PasteSpecial Paste:=xlPasteAll, _
                                              Operation:=xlNone, _
                                              SkipBlanks:=False, _
                                              Transpose:=True
          wsStaging.Range("A2").Value = objDic.Keys()(lngCounter)
          .EntireRow.Delete
        End With
    Next lngCounter
    If .AutoFilterMode Then .Range("A1").AutoFilter
  End With

  Set wsStaging = Nothing
  Set wsData = Nothing

End Sub
I changed the original data to look like
MrE macro-to-always-select-the-first-option-available-in-filtered-column.1213315 220811.xlsm
AB
26KEYVal1
272a
283c
292d
301e
312f
322g
332h
342j
352k
361q
371r
382s
391t
401w
413x
421y
433z
Tabelle1

and got the same result from the code.

Ciao,
Holger
Hello Holger,

Hope you had a great weekend. Apologies for not using the UBB codes, will utilize those moving forward.

Thank you very much for the latest code that you have provided. That is exactly what I was hoping to achieve. You are super awesome!
 
Upvote 0
Hi Chad,

glad that I could help on this topic and thanks for the feedback.

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,215,621
Messages
6,125,884
Members
449,269
Latest member
GBCOACW

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