PDA

View Full Version : [SOLVED:] Filter on Partial string



Aussiebear
05-18-2025, 12:09 AM
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

jdelano
05-18-2025, 02:53 AM
These worked for me... EDIT: added another screen shot resulting in different filtered daya

Paul_Hossler
05-18-2025, 04:34 AM
I'd use 4 helper columns and just filter on them

Aussiebear
05-18-2025, 01:13 PM
Okay , I'll go with that. Thank you