2015-02-10 69 views
0

我在我的Excel工作簿中有兩張表。 1是存貨在表中,1是存貨表。Excel用戶窗體:當滿足多個條件時刪除行

我希望在Stock In中找到數據時,將信息存儲在Stock Out工作表中。

庫存在片:

enter image description here

脫銷片:

enter image description here

例如,缺貨片僅將能夠接受該數據時,PT#和Rack與Sheet In Sheet中的細節相符。

正如下面將是我的我的用戶窗體中刪除按鈕的代碼:

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 
+0

什麼是你的錯誤? – 2015-02-10 08:01:59

+0

它只會刪除PT#但不會檢查機架是否與PT#相符。 – tohobaby 2015-02-10 09:11:11

回答

0

OK - 這是我的理解,什麼下面的代碼調整的作用:

操作員完成一個窗體和投入日期,PT#,機架號和操作員。這在「庫存」表中查找(所有字段必須匹配)。如果操作員確認,則記錄將轉移到下一行可用的「存貨」表中,並從「存貨」表中刪除,其他記錄向上移動。

如果只想說兩個字段匹配(用戶窗體和「股票」),然後請參閱調整,使代碼:評論爲「**

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