Excel VBA:调用从一个子到另一个的结果

我有下面提到的情况下 ,我想要融入不同的function在UserForm而不是粘贴多次。 我怎样才能做到这一点?

  • 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 

    结束小组

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

 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 

你可以改变WsSelector的子function,返回所需的工作表对象的名称?

 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