在程序

时间:2016-11-21 20:01:08

标签: excel vba userform

我正在创建一个程序来使用复选框从UserForm(但会有很多UserForms)制作一些过滤器。该计划的工作方式如下:

  • 从UserForm中选择一些复选框
  • 单击名为Next
  • 的CommandButton
  • 过滤器使用以下代码加载:

Excel VBA代码

Sub Filtrarvar(ByVal j As Integer, ByVal k As Integer, _
               ByVal col As Integer, ByVal Userf As String)

    Dim Countercheck As Integer    
    Countercheck = 0    
    i = j    
    n = j

    Dim Lista() As String    
    longitud = k - j    
    ReDim Lista(longitud)

    Do While i <= k    
        If UserForm2.Controls("CheckBox" & i).Value = True Then    
            ContaTic = ContaTic + 1    
        End If    
        i = i + 1    
    Loop

    If ContaTic = 0 Or ContaTic = longitud + 1 Then    
        Sheets("Aux_total").Range(Cells(1, 1), _
               Cells(UltLinea, UltColumna)).AutoFilter Field:=col, Criteria1:="<>"    
    ElseIf ContaTic >= 1 Then    
        arr = 0    
        Do While n <= k    
            If UserForm2.Controls("CheckBox" & n).Value = True Then    
                Value = UserForm2.Controls("CheckBox" & n).Caption

                Lista(arr) = Value    
                arr = arr + 1    
           End If    
        n = n + 1    
        Loop

        Sheets("Aux_total").Range(Cells(1, 1), _
               Cells(UltLinea, UltColumna)).AutoFilter Field:=col, Criteria1:=Lista, _
               Operator:=xlFilterValues    
    End If    
End Sub

我想这样称呼这个程序:

Filtrarvar 1, 6, 4, "Europe"

我想知道的是如何将代码从UserForm2更改为欧洲等等?我想为每个UserForm / Continent做到这一点。

1 个答案:

答案 0 :(得分:0)

考虑使用行为与集合类似的VBA.Forms。但是,您必须加载现有用户窗体的名称,然后按索引引用它。

Sub Filtrarvar(ByVal j As Integer, ByVal k As Integer, _
               ByVal col As Integer, ByVal Userf As String)

    ' LOAD CURRENT Userf value (i.e., Eurpoe)
    VBA.UserForms.Add Userf
    ...

    ' REFERENCE USERFORM BY INDEX
    Do While i <= k    
        If VBA.UserForms(0).Controls("CheckBox" & i).Value = True Then  
            ContaTic = ContaTic + 1    
        End If    
        i = i + 1    
    Loop
    ...

End Sub