2017-01-09 73 views
0

我已經在組合框中使用了select case語句。請參閱下面的代碼。然而,在我提供的示例中,我一次只能使用一列,我嘗試了幾個示例。如果選擇「當前月份」,則最終結果將使用列「C」和「S」。如何在select case語句中包含多列

嘗試1:

nExtend = 1 'Set this as a default. 
Select Case MonthComboBox.value 
    **Case "Current Month" 
     iCol = "C" 
    Case "Current Month" 
     iCol = "S"** 
    Case "Current Month +1" 
     iCol = "N" 
     nExtend = 5 
    Case "Current Month +2" 
     iCol = "O" 
     nExtend = 4 
    Case "Current Month +3" 
     iCol = "P" 
     nExtend = 3 
    Case "Current Month +4" 
     iCol = "Q" 
     nExtend = 2 
End Select 

嘗試2:

nExtend = 1 'Set this as a default. 
Select Case MonthComboBox.value 
    ****Case "Current Month" 
     iCol = "C" & "S"** 
    Case "Current Month +1" 
     iCol = "N" 
     nExtend = 5 
    Case "Current Month +2" 
     iCol = "O" 
     nExtend = 4 
    Case "Current Month +3" 
     iCol = "P" 
     nExtend = 3 
    Case "Current Month +4" 
     iCol = "Q" 
     nExtend = 2 
End Select 

實際代碼:

Private Sub cmdAdd_Click() 


    Dim irow As Long 
    Dim lastRow As Long 
    Dim iCol As String 
    Dim c As Range 
    Dim ws As Worksheet 
    Dim value As Long 
    Dim NewPart As Boolean 
    Dim ws_warehouse(7) As Worksheet '7 is total warehouse tab you have 
    Dim nExtend As Integer 
    Dim cel As Range 

    Set ws = Worksheets("Main") 

    Set ws_warehouse(1) = Worksheets("Elkhart East") 
    Set ws_warehouse(2) = Worksheets("Tennessee") 
    Set ws_warehouse(3) = Worksheets("Alabama") 
    Set ws_warehouse(4) = Worksheets("North Carolina") 
    Set ws_warehouse(5) = Worksheets("Pennsylvania") 
    Set ws_warehouse(6) = Worksheets("Texas") 
    Set ws_warehouse(7) = Worksheets("West Coast") 

    Set c = ws.Range("A7:A1048576").Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _ 
      SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole) 
    If c Is Nothing Then 
    'find first empty row in database 
     lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count 
     irow = lastRow + 1 
     NewPart = True 
    Else 
    'find row where the part is 
     irow = ws.Cells.Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _ 
      SearchDirection:=xlPrevious, LookIn:=xlValues).Row 
     NewPart = False 
    End If 

    'check for a part number 
    If Trim(Me.PartTextBox.value) = "" Then 
     Me.PartTextBox.SetFocus 
     MsgBox "Please Enter A Part Number" 
     Exit Sub 
    End If 

    If Trim(Me.MonthComboBox.value) = "" Then 
     Me.MonthComboBox.SetFocus 
     MsgBox "Please Enter A Month" 
     Exit Sub 
    End If 

    If Trim(Me.AddTextBox.value) = "" Then 
     Me.AddTextBox.SetFocus 
     MsgBox "Please Enter A Value To Add Or Substract" 
     Exit Sub 
    End If 

    nExtend = 1 'Set this as a default. 
    Select Case MonthComboBox.value 
     Case "Current Month" 
      iCol = "C" 

     Case "Current Month +1" 
      iCol = "N" 
      nExtend = 4 
     Case "Current Month +2" 
      iCol = "O" 
      nExtend = 3 
     Case "Current Month +3" 
      iCol = "P" 
      nExtend = 2 
     Case "Current Month +4" 
      iCol = "Q" 
    End Select 

    actWarehouse = Me.warehousecombobox.ListIndex + 1 

    With ws 
     .Cells(irow, "A").value = Me.PartTextBox.value 


     For Each cel In .Cells(irow, iCol).Resize(, nExtend) 
      cel.value = cel.value + CLng(Me.AddTextBox.value) 
      cel.Interior.ColorIndex = 6 
     Next cel 
    End With 

    With ws_warehouse(actWarehouse) 
     'find part number 
     l_row = .Range("A" & .Rows.Count).End(xlUp).Row 

     NewPart = True 
     For r = 7 To l_row 
      If Trim(.Range("A" & r)) = "" Then Exit For 
      If LCase(.Range("A" & r)) = LCase(Me.PartTextBox.Text) Then 
       irow = r 
       Exit For 
       NewPart = False 
      End If 
     Next r 
     If NewPart Then irow = r 

     .Cells(irow, "A").value = Me.PartTextBox.value 
     For Each cel In .Cells(irow, iCol).Resize(, nExtend) 
      cel.value = cel.value + CLng(Me.AddTextBox.value) 
      cel.Interior.ColorIndex = 6 
     Next cel 
    End With 

    'clear the data 
    Me.PartTextBox.value = "" 
    Me.MonthComboBox.value = "" 
    Me.AddTextBox.value = "" 
    Me.PartTextBox.SetFocus 
    Me.warehousecombobox.value = "" 

End Sub 

Private Sub cmdClose_Click() 
    Unload Me 
End Sub 
+0

UNION應該幫助'union(range(「a1:a3」),range(「c 1:c3「))',如果這些是標題,則使用Application.Worksheetfunction.Match(」header val「,」headers「,0)'作爲第一個標題的偏移量。因此,如果標題爲a1:J1,則會從a1(匹配-1)偏移,因此對於col C,匹配將返回3,偏移a1 3-2,= c1。看看'range(「A1」)。offset(0,Application.WorksheetFunction.MATCH(「Header」,range(「A1:J1」),0))。地址' –

+0

@Nathan_Sav我試過Union我收到調試錯誤_全局失敗。 nExtend = 1'將其設置爲默認值。 選擇事例MonthComboBox.value 案 「當前月」 ICOL =聯盟(範圍( 「C」),範圍( 「S」)) 案 「當前月1」 ICOL = 「N」 下一頁末頁= 5 案 「當前月2」 ICOL = 「O」 下一頁末頁= 4 案 「當前月3」 ICOL = 「P」 下一頁末頁= 3 案 「當前月4」 ICOL = 「Q」 nExtend = 2 結束選擇 – Luis

+0

範圍(「C:C」)不是單個C,C完成時的起始範圍C –

回答

1

我建議你改變你的選擇情況聲明如下:

Select Case MonthComboBox.value 
    Case "Current Month" 
     iCol = "C:C,S:S" 
    Case "Current Month +1" 
     iCol = "N:R" 
    Case "Current Month +2" 
     iCol = "O:R" 
    Case "Current Month +3" 
     iCol = "P:R" 
    Case "Current Month +4" 
     iCol = "Q:R" 
End Select 

(你不需要nExtend - 它是構建在您的iCol設置列規格)

然後你For Each cel循環都可以改爲

For Each cel In Intersect(.Rows(irow), .Range(iCol)) 

你完整修改後的代碼如下:

Private Sub cmdAdd_Click() 


    Dim irow As Long 
    Dim lastRow As Long 
    Dim iCol As String 
    Dim c As Range 
    Dim ws As Worksheet 
    Dim value As Long 
    Dim NewPart As Boolean 
    Dim ws_warehouse(7) As Worksheet '7 is total warehouse tab you have 
    Dim cel As Range 

    Set ws = Worksheets("Main") 

    Set ws_warehouse(1) = Worksheets("Elkhart East") 
    Set ws_warehouse(2) = Worksheets("Tennessee") 
    Set ws_warehouse(3) = Worksheets("Alabama") 
    Set ws_warehouse(4) = Worksheets("North Carolina") 
    Set ws_warehouse(5) = Worksheets("Pennsylvania") 
    Set ws_warehouse(6) = Worksheets("Texas") 
    Set ws_warehouse(7) = Worksheets("West Coast") 

    Set c = ws.Range("A7:A1048576").Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _ 
      SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole) 
    If c Is Nothing Then 
    'find first empty row in database 
     lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count 
     irow = lastRow + 1 
     NewPart = True 
    Else 
    'find row where the part is 
     irow = ws.Cells.Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _ 
      SearchDirection:=xlPrevious, LookIn:=xlValues).Row 
     NewPart = False 
    End If 

    'check for a part number 
    If Trim(Me.PartTextBox.value) = "" Then 
     Me.PartTextBox.SetFocus 
     MsgBox "Please Enter A Part Number" 
     Exit Sub 
    End If 

    If Trim(Me.MonthComboBox.value) = "" Then 
     Me.MonthComboBox.SetFocus 
     MsgBox "Please Enter A Month" 
     Exit Sub 
    End If 

    If Trim(Me.AddTextbox.value) = "" Then 
     Me.AddTextbox.SetFocus 
     MsgBox "Please Enter A Value To Add Or Substract" 
     Exit Sub 
    End If 

    Select Case MonthComboBox.value 
     Case "Current Month" 
      iCol = "C:C,S:S" 
     Case "Current Month +1" 
      iCol = "N:R" 
     Case "Current Month +2" 
      iCol = "O:R" 
     Case "Current Month +3" 
      iCol = "P:R" 
     Case "Current Month +4" 
      iCol = "Q:R" 
    End Select 

    actWarehouse = Me.warehousecombobox.ListIndex + 1 

    With ws 
     .Cells(irow, "A").value = Me.PartTextBox.value 


     For Each cel In Intersect(.Rows(irow), .Range(iCol)) 
      cel.value = cel.value + CLng(Me.AddTextbox.value) 
      cel.Interior.ColorIndex = 6 
     Next cel 
    End With 

    With ws_warehouse(actWarehouse) 
     'find part number 
     l_row = .Range("A" & .Rows.Count).End(xlUp).Row 

     NewPart = True 
     For r = 7 To l_row 
      If Trim(.Range("A" & r)) = "" Then Exit For 
      If LCase(.Range("A" & r)) = LCase(Me.PartTextBox.Text) Then 
       irow = r 
       Exit For 
       NewPart = False 
      End If 
     Next r 
     If NewPart Then irow = r 

     .Cells(irow, "A").value = Me.PartTextBox.value 
     For Each cel In Intersect(.Rows(irow), .Range(iCol)) 
      cel.value = cel.value + CLng(Me.AddTextbox.value) 
      cel.Interior.ColorIndex = 6 
     Next cel 
    End With 

    'clear the data 
    Me.PartTextBox.value = "" 
    Me.MonthComboBox.value = "" 
    Me.AddTextbox.value = "" 
    Me.PartTextBox.SetFocus 
    Me.warehousecombobox.value = "" 

End Sub 

Private Sub cmdClose_Click() 
    Unload Me 
End Sub