根据提供的值将用户表单中的组合框合并到多个工作表中

时间:2016-04-20 13:33:43

标签: excel vba excel-vba combobox

我对这部分感到有些困惑。

我创建了一个包含不同部分的用户表单。第一部分将询问部件#,如果部件存在,则用户表将在A列中搜索,如果部件不存在,则将其添加到可用的最后一行。第二部分是组合框,选择一个月来添加值(C列,N,O,P,Q列)。下一部分是基于选择的月份给予零件的价值金额。下一节是我被困在仓库部分的地方我想要选择一个仓库,这个仓库引用我创建的标签“Elkhart”,“Tennessee”等。我希望能够在活动表中添加部件,这是“main”并且还基于仓库组合框选项将其添加到选项卡的最后一行。

  

实施例。部分:鲍勃

     

月:电流

     

添加:200

     

仓库:Elkhart East

这会将 Bob 添加到lastrow中的“ Main ”标签中,但会将其添加到“ Elkhart East ”标签中在最后一行

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
Set ws = Worksheets("Main")
Set wsE = Worksheets("Elkhart East")
Set wsT = Worksheets("Tennessee")
Set wsA = Worksheets("Alabama")
Set wsN = Worksheets("North Carolina")
Set wsP = Worksheets("Pennsylvania")
Set wsT = Worksheets("Texas")
Set wsW = 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"

    Case "Current Month +1"

        iCol = "N"

    Case "Current Month +2"

        iCol = "O"

    Case "Current Month +3"

        iCol = "P"

    Case "Current Month +4"

        iCol = "Q"

End Select

**Select Case warehousecombobox.value
    Case "Elkhart East"
        iCol = wsE And irow
    Case "Tennessee"
        iCol = wsT And irow
    Case "Alabama"
        iCol = wsA And irow
    Case "North Carolina"
        iCol = wsN And irow
    Case "Pennsylvania"
        iCol = wsP And irow
    Case "Texas"
        iCol = wsT And irow
    Case "West Coast"
        iCol = wsW And irow
    End Select
value = Cells(irow, iCol).value**
With ws


  .Cells(irow, iCol).value = value + CLng(Me.AddTextBox.value)


End With

If NewPart = True Then
    ws.Cells(irow, "A").value = Me.PartTextBox.value
End If


If NewPart = True Then
 ws.Cells(irow, "C").value = Me.AddTextBox.value
End If



'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


Private Sub MonthComboBox_Change()

End Sub
Private Sub Warehousecombobox_Change()

End Sub

Private Sub UserForm_Initialize()

'Empty NameTextBox
 PartTextBox.value = ""

'Empty PhoneTextBox
 AddTextBox.value = ""

'Empty DinnerComboBox

'Fill DinnerComboBox
With MonthComboBox
     .AddItem "Current Month"
     .AddItem "Current Month +1"
     .AddItem "Current Month +2"
     .AddItem "Current Month +3"
     .AddItem "Current Month +4"

End With

With warehousecombobox
    .AddItem "Elkhart East"
    .AddItem "Tennessee"
    .AddItem "Alabama"
    .AddItem "North Carolina"
    .AddItem "Pennsylvania"
    .AddItem "Texas"
    .AddItem "West Coast"
End With
End Sub

1 个答案:

答案 0 :(得分:0)

好吧也许我没有得到你的代码,但我认为你想要这样的东西:

**Select Case warehousecombobox.value
Case "Elkhart East"
    wSheet = wsE 
...

wSheet.Range(iCol & iRow).value =  CLng(Me.AddTextBox.value)