我尝试选择年份和业务类型(1个或更多),然后自动过滤列。这样我只能使用1个宏而不是为所有替代品制作许多宏。
(Year Select)
(Type of Business)
这就是我现在所拥有的:
Private Sub Botton1_Click()
Public Platform As String
Public Year as Integer
Platform = UserForm1.LB2.Text
Year = UserForm1.LB1.value
Unload UserForm1
End Sub
...
Private Sub UserForm_Initialize()
With LB1
.AddItem "2016"
.AddItem "2017"
.AddItem "2018"
End With
With LB2
.AddItem "CMP"
.AddItem "AS"
.AddItem "MasterBread"
.AddItem "CMI -Andino"
.AddItem "CMI -Brasil"
.AddItem "CMI -CAMEC"
.AddItem "CMI -ConoSur"
.AddItem "Global"
End With
End Sub
年份总是只有1个值,但业务类型可以是1个或更多。
如何存储列表框的多个值以便将变量作为过滤器调用?
这是我需要调用变量:
ActiveSheet.Range("$A$1:$G$1500").AutoFilter Field:=4, Criteria1:="2016"
ActiveSheet.Range("$A$1:$G$1500").AutoFilter Field:=2, Criteria1:="=AS", _
Operator:=xlOr, Criteria2:="=MASTER BREAD"
在这个例子中我需要过滤" 2016"和" AS& MASRE BREAD"。
答案 0 :(得分:0)
根据您在问题中提供的内容,您的按钮应使用的注释代码:
Private Sub Botton1_Click()
Dim rData As Range
Dim sFilters As String
Dim i As Long
Set rData = ActiveSheet.Range("$A$1:$G$1500")
rData.AutoFilter 'Remove any existing filters
'Get the selected year
If LB1.ListIndex > -1 Then
'Filter the year
rData.AutoFilter 4, LB1.Text
End If
'Gather all selected values in LB2 (type of business)
For i = 0 To LB2.ListCount - 1
'If value is selected, add to sFilters variable
If LB2.Selected(i) Then sFilters = sFilters & "|" & LB2.List(i)
Next i
If Len(sFilters) > 0 Then
'Filter on selected values
rData.AutoFilter 2, Split(sFilters, "|"), xlFilterValues
End If
End Sub