Excel Userform:满足多个条件时删除行

时间:2015-02-10 07:12:05

标签: excel vba excel-2010

我的Excel工作簿上有2张。 1是Stock in sheet,1是Stock Out sheet。

我希望在Stock In。

中找到数据时将信息存储在Stock Out表格中

股票在表格中:

enter image description here

Stock Out sheet:

enter image description here

例如,只有在PT#和Rack与Stock In表中的详细信息相符时,Stock Out表才能接受数据。

以下是我的userform中删除按钮的代码:

 Private Sub TrackOut_Click()
  Sheets("Stock Out").Activate
  Dim cDelete As VbMsgBoxResult
               With Me
                        If Len(.TextBox1.Value) * Len(.PT.Value) *      
   Len(.Rack2.Value) * _
            Len(.Operator2.Value) = 0 Then
        MsgBox "Please Complete All Fields Before Submit"
    Else

    cDelete = MsgBox("Are you sure that you want to delete this record", vbYesNo + vbDefaultButton2, "Track Out")
    If cDelete = vbYes Then
       If Sheets("Stock In").Columns(2).Find(What:=PT.Text) Is Nothing Then
          MsgBox "No stock inventory for this PT#"
          Exit Sub
       End If
       eRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Cells(eRow, 1).Value = TextBox1.Text
        Cells(eRow, 2).Value = PT.Text
        Cells(eRow, 3).Value = Rack2.Text
        Cells(eRow, 4).Value = Operator2.Text

        Else

        If cDelete = vbNo Then
        Unload Me
        End If
        End If
    End If
End With

End Sub

1 个答案:

答案 0 :(得分:0)

好的 - 这就是我理解的以及下面的代码调整:

操作员完成用户表格并输入日期,PT#,机架号和操作员。这会在' Stock In'表(所有字段必须匹配)。如果运营商确认,那么该记录将被转移到' Stock Out'下一个可用行上的工作表并从' Stock In'中删除表格和其他记录向上移动。

如果您只想说两个字段匹配(userform和' Stock In'),请查看要进行调整的代码:注释为' **

Private Sub TrackOut_Click()

Sheets("Stock Out").Activate
Dim cDelete As VbMsgBoxResult
Dim fndItm As Range
Dim orow As Long, irow As Long
Dim reqStock As String, itmStock As String
Dim stockArr(4) As String
    With Me
        If Len(.TextBox1.Value) * Len(.PT.Value) * Len(.Rack2.Value) * Len(.Operator2.Value) = 0 Then
            MsgBox "Please Complete All Fields Before Submit."
        Else
            'collect requested (userform) data
            '** reqStock should include those fields you require to match with Stock In record
            '** currently set to check all fields
            reqStock = .TextBox1.Value & .PT.Value & .Rack2.Value & .Operator2.Value
            Set fndItm = Sheets("Stock In").Columns(2).Find(What:=PT.Text)
                If Not fndItm Is Nothing Then
                    'if PT# found collect stock row data
                    With Sheets("Stock In")
                        irow = fndItm.Row
                        stockArr(0) = Format(.Cells(irow, 1).Value, "dd/mm/yyyy")
                        stockArr(1) = .Cells(irow, 2).Value
                        stockArr(2) = .Cells(irow, 3).Value
                        stockArr(3) = .Cells(irow, 4).Value
                        '** itmStock should include those fields you require to match with userform fields
                        '** these should match reqStock
                        '** currently set to check all fields
                        itmStock = stockArr(0) & stockArr(1) & stockArr(2) & stockArr(3)
                    End With
                    'compare requested (userfrom) data with Stock In data
                    If reqStock = itmStock Then
                        cDelete = MsgBox("Are you sure that you want to delete this record from stock?", vbYesNo) ' + vbDefaultButton2, _
                        "Track Out")
                            If cDelete = vbYes Then
                                'xfer record to Stock Out sheet
                                With Sheets("Stock Out")
                                    orow = .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                                    .Range(.Cells(orow, 1), .Cells(orow, 4)) = stockArr
                                End With
                                'delete record from Stock In sheet
                                With Sheets("Stock In")
                                    .Range(.Cells(irow, 1), .Cells(irow, 4)).Delete xlShiftUp
                                End With
                            End If
                        'clear userform fields for next entry
                        .TextBox1.Value = ""
                        .PT.Value = ""
                        .Rack2.Value = ""
                        .Operator2.Value = ""
                    Else
                        MsgBox "PT# found but requested information does not match."
                    End If
                Else
                    MsgBox "No stock inventory for this PT#."
                    Exit Sub
                End If
        End If
    End With
End Sub