将userform Value添加到多个列

时间:2016-05-04 18:05:44

标签: excel vba excel-vba

嘿伙计们这个人很复杂。我创建了一个userform,它将用户的值添加到某个列(这是当前代码)。但是,我不想将其添加到该列,而是希望将其添加到后续列中。例如,如果我要选择当前+1和值= 500,我希望该值将其添加到列N和随后的" O"," P" " Q&#34 ;.我认为将它添加到案例中会很容易,但事情并不那么容易

        Case "Current Month +1"
            iCol = "N" and "O" and "P" and "Q"

        Case "Current Month +2"
            iCol = "O" and "P" and "Q"

****这不容易

Private Sub cmdAdd_Click()
    If TrialVersion Then Exit Sub

    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

    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"

        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

    actWarehouse = Me.warehousecombobox.ListIndex + 1

    With ws
        .Cells(irow, "A").value = Me.PartTextBox.value
        .Cells(irow, iCol).value = .Cells(irow, iCol).value + CLng(Me.AddTextBox.value)
    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
        .Cells(irow, iCol).value = .Cells(irow, iCol).value + CLng(Me.AddTextBox.value)
    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

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 :(得分:1)

编辑包含所有源代码:

Private Sub cmdAdd_Click()
    If TrialVersion Then Exit Sub

    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)
        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
        .Cells(irow, iCol).value = .Cells(irow, iCol).value + CLng(Me.AddTextbox.value)
    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

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

如果列始终是连续的,也许您可​​以添加第二个integer变量,该变量告诉代码要包含多少列。例如,在您的帖子中,您显示Current Month + 1Column N开始,并向右延伸3列(O,P,Q)。您的代码可能类似于:

Select Case MonthComboBox.value
    Case "Current Month + 1"
        iCol = "C"
        nExtend = 4 

然后当你处理这些值时,代码看起来像是:

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

注意:即使您在技术上扩展了3列,Resize函数的输入也是4.还要记下前导逗号,因为我们没有更改行调整大小。

基于新信息的编辑:

使用循环更改范围中的每个值,例如:

Select Case MonthComboBox.value
    Case "Current Month + 1"
        iCol = "C"
        nExtend = 4 

For Each cel in .Cells(irow, iCol).Resize(,nExtend)
    cel.Value = cel.Value + CLng(Me.AddTextBox.Value)
Next cel