Consulting

Results 1 to 4 of 4

Thread: Filter on Partial string

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,427
    Location

    Filter on Partial string

    Had hoped to be able to filter the table based on a partial string in Column C. Column C contains IP addresses, and I'm currently needing to filter based on data such as 145. or 145.74. or 145.74.8. instead of the full IP address.

    The following code does not function as intended
    Sub FilterIPAddresses()
      Dim filterValue As String
      Dim lastRow As Long
      Dim ws As Worksheet
      ' Set the worksheet
      Set ws = ThisWorkbook.Sheets("Sheet1")
      ' Get the last row with data in Column C
      On Error Resume Next ' In case Column C is empty
      lastRow = ws.Cells(Rows.Count, "C").End(xlUp).Row
      On Error GoTo 0 ' Turn error handling back on
      ' Prompt the user for the IP address or partial IP address to filter by
      filterValue = InputBox("Enter the full or partial IP address to filter by:", "Filter IP Addresses")
      ' Check if the user clicked Cancel
      If filterValue = "" Then
        Exit Sub
      End If
      ' Clear any existing filters on the worksheet
      ws.AutoFilterMode = False
      ' Apply the filter to Column C
      With ws.Range("C1:C" & lastRow)
        .AutoFilter Field:=1, Criteria1:="*" & filterValue & "*"
      End With
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  2. #2
    These worked for me... EDIT: added another screen shot resulting in different filtered daya
    Attached Images Attached Images

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,840
    Location
    I'd use 4 helper columns and just filter on them
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,427
    Location
    Okay , I'll go with that. Thank you
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •