我是VBA的新手,正在开展一个项目,我必须在两个特定值(4400000000和5600000000)之间过滤数据。这对我来说变得棘手,是5500000000的一些人在他们后面有信件,即5500000000 CST。我如何编写我的代码,以便在它们背后有字母时包含这些数字。如果这太模糊了,我怎么能写我的代码所以它包括CST的?到目前为止,这是我的代码:
Sub macro5()
Worksheets("info1").Range("A1").AutoFilter _
Field:=7, _
Criteria1:=">=" & 4400000000#, Operator:=xlFilterValues, Criteria2:="<5600000000, Operator:=xlFilterValues"
End Sub
答案 0 :(得分:2)
循环并创建要过滤的值的字典,然后将键用作标准1和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