2014-10-09 56 views
1

我有下面提到的case我想要集成到不同的sub在UserForm中運行,而不是粘貼多次。我怎樣才能做到這一點?Excel VBA:調用從一個子到另一個的結果

  • GSMListType是一個組合框
  • 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 
+0

對不起,但你是否要求將'WsSelector'移動到多個潛艇而不需要複製並粘貼到潛艇? – BradyK 2014-10-09 13:59:10

+0

@BradyK是的,正好。 – 2014-10-09 16:32:46

回答

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 
相關問題