Excel VBA:将一个案例结果从一个子句调用到另一个子项

时间:2014-10-09 13:43:32

标签: excel vba excel-vba

我有以下提到的案例,我希望将其集成到UserForm中不同的 sub ,而不是多次粘贴。我怎么能这样做?

  • GSMListType是一个ComboBox
  • AvailableNumberList是一个列表框

    Sub WsSelector()
                Dim WSLookup as WorkSheet
                With GSMListType
                Select Case .Value
                    Case "A"
                        Set WSLookup = A_Regular
                    Case "A - K"
                        Set WSLookup = A_K
                    Case "A - MOT"
                        Set WSLookup = A_MOT
                        Case "B"
                        Set WSLookup = B_Regular
                    Case "C"
                        Set WSLookup = C_Regular
                    Case "D"
                        Set WSLookup = D_Regular
                    Case "D - DATA"
                        Set WSLookup = D_DATA
                    Case "D - MOT"
                        Set WSLookup = D_MOT
                    Case "E"
                        Set WSLookup = E_Regular
                    Case "F"
                        Set WSLookup = F_Regular
                    Case "G"
                        Set WSLookup = G_Regular
                    Case "H"
                        Set WSLookup = H_Regular
                    Case "I"
                        Set WSLookup = I_Regular
                    Case "J"
                        Set WSLookup = J_Regular
                    Case "J - DATA"
                        Set WSLookup = J_DATA
                    Case "K"
                        Set WSLookup = K_Regular
                    Case "L"
                        Set WSLookup = L_Regular
                    Case "M"
                        Set WSLookup = M_Regular
                    Case "N"
                        Set WSLookup = N_Regular
                    Case "O"
                        Set WSLookup = O_Regular
                    Case "P"
                        Set WSLookup = P_Regular
                    Case "P - MOT"
                        Set WSLookup = P_MOT
                    Case "Q"
                        Set WSLookup = Q_Regular
                    Case "R"
                        Set WSLookup = R_Regular
                    Case "S"
                        Set WSLookup = S_Regular
                    Case "T"
                        Set WSLookup = T_Regular
                    Case "U"
                        Set WSLookup = U_Regular
                    Case "V"
                        Set WSLookup = V_Regular
                    Case "W"
                        Set WSLookup = W_Regular
                    Case "X"
                        Set WSLookup = X_Regular
                    Case "Y"
                        Set WSLookup = Y_Regular
                    Case "Z"
                        Set WSLookup = Z_Regular
                End Select
            End With
    

    End Sub

上面提到的代码应该导入到不同的子代码中,其中一个代码包含下面提到的代码:

Private Sub GSMListType_Change()
    Dim TypeLookup As Long, WSLookup As Worksheet
    'If listing has changed, clear AvailableNumberList and insert new data
    If GSMListType.ListIndex > -1 Then

Call SheetSelector

TypeLookup = Application.CountIf(WSLookup.Range("A:E"), GSMListType.Value)

        With AvailableNumberList
            .Clear
            For k = 2 To TypeLookup + 1
                .AddItem WSLookup.Range("A" & k).Value
            Next k
        End With
    End If
    Set WSLookup = Nothing
End Sub

1 个答案:

答案 0 :(得分:2)

您可以将WsSelector子更改为返回所需工作表对象名称的函数吗?

Function WsSelector(sTYP As String)
    Select Case sTYP
        Case "A"
            WsSelector = A_Regular.Name
        Case "A - K"
            WsSelector = A_K.Name
        Case "A - MOT"
            WsSelector = A_MOT.Name
        Case "Etc..."
            'Etc...
            'Etc...
        Case Else
            WsSelector = vbNullString
    End Select
End Function

现在可以在设置所选工作表时调用此方法。

Private Sub GSMListType_Change()
    Set WSLookup = Sheets(WsSelector(GSMListType.Value))
    'lots of code here
    Set WSLookup = Nothing
End Sub