I am new to VBA and working on a project in which I have to filter data between two specific values (4400000000 and 5600000000). Where this gets tricky for me, is that some of the 5500000000’s have letters behind them, i.e. 5500000000 CST. How can I write my code so it includes these numbers if there are letters behind them. If that is too vague, how can I write my code so it includes the CST’s? Here is my code so far:
Sub macro5()
Worksheets("info1").Range("A1").AutoFilter _
Field:=7, _
Criteria1:=">=" & 4400000000#, Operator:=xlFilterValues, Criteria2:="<5600000000, Operator:=xlFilterValues"
End Sub
Loop through and create a dictionary of the values to filter then use the keys as criteria1 with xlfiltervalues.
Option Explicit
Sub macro5()
Dim d As Long, dict As Object
Dim i As Double, mn As Double, mx As Double
Set dict = CreateObject("scripting.dictionary")
mn = 4400000000#
mx = 5600000000#
With Worksheets("info1")
If .AutoFilterMode Then .AutoFilterMode = False
For d = 2 To .Cells(.Rows.Count, "G").End(xlUp).Row
If IsNumeric(Left(.Cells(d, "G").Value2, 1)) Then
i = CDbl(Split(.Cells(d, "G").Value2 & Chr(32), Chr(32))(0))
If i >= mn And i < mx Then
dict.Item(CStr(.Cells(d, "G").Value2)) = .Cells(d, "G").Value2
End If
End If
Next d
.Range("A1").AutoFilter Field:=7, Criteria1:=dict.keys, Operator:=xlFilterValues
End With
End Sub
Tags: excelexcel, exception, filter, vba