Excel用户表单:满足多个条件时删除行
我的excel工作簿上有两张工作表。 1为入库单,1为出库单 我希望在库存中找到数据时将信息存储在库存表中 入库单: 出库单: 例如,只有当PT#和机架与入库单中的细节相符时,出库单才能接受数据 下面是我的userform中“删除”按钮的代码:Excel用户表单:满足多个条件时删除行,excel,vba,excel-2010,Excel,Vba,Excel 2010,我的excel工作簿上有两张工作表。 1为入库单,1为出库单 我希望在库存中找到数据时将信息存储在库存表中 入库单: 出库单: 例如,只有当PT#和机架与入库单中的细节相符时,出库单才能接受数据 下面是我的userform中“删除”按钮的代码: Private Sub TrackOut_Click() Sheets("Stock Out").Activate Dim cDelete As VbMsgBoxResult With Me
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
好的-这就是我的理解,下面的代码调整就是这么做的: 操作员填写用户表单并输入日期、PT#、机架号和操作员。这将在“库存”表中查找(所有字段必须匹配)。如果操作员确认,则记录将转移到下一可用行的“出库”表中,并从“入库”表中删除,其他记录则上移 如果您只想说两个字段匹配(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
它只会删除PT,但不会检查机架是否与PT相符。