Select multiple values in column

msvoboda27

New Member
Joined
Feb 9, 2018
Messages
30
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello, I have 1400 rows in my spreadsheet and I would like to know if there is a way to select multiple values in a sort column. I know I can manually select each value, but it is time-consuming. I looked at the number filter function to sort between two values, but there are still many values between the ones that need to be sorted.

Below is a sample list of job numbers I want to filter for. These are only some of the numbers.
8012764 8012773 8013180 8013257 8013258 8013271 8013272 8013274 8013276 8013277 8013278

I was hoping to do a CSV export for the values, but I cannot get this to work. If it's an option to do this with VBA, I'm not sure how to do it.

Can someone please help?
Thank you!
Mic
Sample Of Excel File to Filter.png
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I use this code all the time. You copy a range of numbers, put your cursor in the column you want to filter and run the macro. It will filter the copied items.

It is best you turn on your autofilter and freeze panes so the macro knows where the headers are. Give it a try and let me know if it works for you.


VBA Code:
'This will filter multiple selected items in a single column that you select
'Copy a cell with several items separated by a comma or (comma space)
'You may run this again with a different set of criteria on the same or a different column
'Autofilter will be activated if off
'Filter Criteria requires the number formatting to be correct with all commas and dollar signs
'This assumes the number format is the same for the whole column
'This will add the positive/negative opposite of a number in the criteria
'This works with text just the same
Sub AutoFilterCopiedItems()
  Dim Rng As Range
  Dim Col As Range
  Dim Sel As Range
  Dim Sht As Worksheet
  Dim Fld As Long
  Dim Cel As Range
  Dim Ary As Variant
  Dim aStr As String
  Dim vStr As Variant
  Dim RngArea As Range
  Dim NF As String
  Dim X As Long
  Dim Val As Double
  Dim Changes As Long
  Dim vErr As Boolean
  Dim App As Application
  Dim WF As WorksheetFunction
  Dim LO As ListObject
  Dim xNF As String
  Dim NFType As String
  Dim Headers As Range
  Dim LastRow As Long
  
  
  Set Sht = ActiveSheet
  Set Sel = Selection
  Set Col = Sel.EntireColumn
  If Col.Columns.Count > 1 Then                               'User selected more than one column
    MsgBox "Please only select items in one column to filter"
    Exit Sub
  End If
  
  '--------- Find the range that will be filtered ---------------------------------
  On Error Resume Next
  Set Rng = Sht.AutoFilter.Range                              'Autofilter must be on
  On Error GoTo 0
  If Rng Is Nothing Then
    Set LO = Sel.ListObject                                   'Structured Excel Table
    If Not LO Is Nothing Then Set Rng = LO.DataBodyRange
  End If
  If Rng Is Nothing Then
    With ActiveWindow
      Set Cel = Sht.Cells(.SplitRow, Sel.Column)                'Row above the Freeze Panes line
    End With
    If Cel.Column > 1 Then
      If Cel.Offset(0, -1) <> "" And Cel.Offset(0, 1) <> "" Then
        Set Headers = Sht.Range(Cel.End(xlToLeft), Cel.End(xlToRight))
      ElseIf Cel.Offset(0, -1) = "" And Cel.Offset(0, 1) <> "" Then
        Set Headers = Sht.Range(Cel, Cel.End(xlToRight))
      ElseIf Cel.Offset(0, -1) <> "" And Cel.Offset(0, 1) = "" Then
        Set Headers = Sht.Range(Cel.End(xlToLeft), Cel)
      End If
    ElseIf Cel.Column = 1 Then
      If Cel.Offset(0, 1) <> "" Then
        Set Headers = Sht.Range(Cel, Cel.End(xlToRight))
      ElseIf Cel.Offset(0, 1) = "" Then
        Set Headers = Cel
      End If
    End If
    X = 0
    If Not Headers Is Nothing Then
      For Each Cel In Headers
        X = Sht.Cells(Sht.Cells.Rows.Count, Cel.Column).End(xlUp).Row
        If X > LastRow Then LastRow = X
      Next Cel
      Set Cel = Headers.Resize(1, 1).Offset(0, Headers.Columns.Count - 1)
      Set Rng = Sht.Range(Headers.Resize(1, 1), Intersect(Sht.Cells(LastRow, 1).EntireRow, Cel.EntireColumn))
    End If
      
  End If
  
  If Rng Is Nothing Then                                      'Current Region is less accurate
    Set Rng = Sel.CurrentRegion
    Rng.AutoFilter
  End If
  '---------------------------------------------------------------------------------
  Fld = Col.Column - Rng.Resize(1, 1).Column + 1              'Field number in filtered range
  
  xNF = "mdy/[]hms:apE+"                        'Characters to screen out non-numbers
  NF = Sel.Areas(1).Resize(1, 1).NumberFormat                 'Get number format of first cell
  NFType = "Number"                                           'Start as a number
  If NF = "@" Then                                            'Text
    NFType = "Text"
  ElseIf NF = "General" Then                                  'Not Formatted
    NFType = "General"
  Else
    For X = 1 To Len(xNF)
      If InStr(NF, Mid(xNF, X, 1)) > 0 Then                   'Found a character that might be a date or time
        NFType = "Text"
        Exit For
      End If
    Next X
  End If
  
  
  vStr = CopyFromClipboard
'  For X = 1 To Len(vStr)
'    Debug.Print Asc(Mid(vStr, X, 1))
'  Next X
  
  If InStr(vStr, ",") > 0 Then
    vStr = Replace(vStr, ", ", ",")                'remove spaces after comma
    Ary = Split(vStr, ",")                           'load into array
  Else
    vStr = Replace(vStr, vbCr, "")
    Ary = Split(vStr, vbLf)                           'load into array
  End If
  
  For X = 0 To UBound(Ary)
    vErr = False                                              'reset vErr
    On Error GoTo HolyErrorBatman                                 'Turn on error checking
    Val = Ary(X)                                           'Change to number - may get an error if text
    On Error GoTo 0
    
    If NFType = "Text" Or vErr = True Then                      'Text or Value Error
      aStr = aStr & Chr(1) & Ary(X)
    ElseIf NFType = "General" Then                              'No value error
      aStr = aStr & Chr(1) & Val
      aStr = aStr & Chr(1) & Val * -1                           'Opposite General
    ElseIf NFType = "Number" Then                               'still no value error
      aStr = aStr & Chr(1) & Format(Val, NF)                    'Formatted number
      aStr = aStr & Chr(1) & Format(Val * -1, NF)                'Opposite formatted number
    End If
  Next X
  Ary = Split(Mid(aStr, 2), Chr(1))                                     'load into array again after first chr(1)
  Rng.AutoFilter Field:=Fld, Criteria1:=Ary, Operator:=xlFilterValues   'Filter sheet
        
  Exit Sub
  
HolyErrorBatman:
  vErr = True
  Resume Next

End Sub
 
Upvote 0
Below is a sample list of job numbers I want to filter for.
Not sure if you have those numbers stored in a worksheet somewhere or you want them directly in the code but see if this is any help.
I have assumed from your image that AutoFilter is already set up in the worksheet.

VBA Code:
Sub Filter_Values()
  Const myVals As String = "8012764 8012773 8013180 8013257 8013258 8013271 8013272 8013274 8013276 8013277 8013278"
  
  ActiveSheet.AutoFilter.Range.AutoFilter Field:=2, Criteria1:=Split(myVals), Operator:=xlFilterValues
End Sub

BTW, I suggest that you update your forum profile (click your user name at the top right of the forum, then ‘Account details’) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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