我建了一个表来跟踪我在网上订购的商品。它存储物品的名称和相应的信息(订购数量,包括的件数,订购日期,发货日期,运输状态,购买来源),并为每个项目分配一个ID。
表格旁边有一个按钮,我可以点击该按钮,选择是否向列表中添加新项目或编辑现有项目信息(发货状态等)。我能够在列表中添加一个新项目,并且我能够编辑除表格中最后一项之外的所有项目的信息。
当我选择要编辑的项目时,userform会显示相应的信息。这适用于我的表格中的每个项目,除了最后一个项目,项目#55显示与选择的最后一个项目相同的信息,或者如果首先选择则没有任何内容。
我尝试手动清除每个单元格,然后使用表单重新添加项目,它们被正确添加,但是当我尝试编辑时,信息仍然无法显示。
在下面的代码中,删除按钮的子程序已被注释掉,如果有人对我需要做什么的建议以使其工作,那也将受到赞赏,但我会在那里开始一个新的问题。未来,如果我需要。
真正的问题是让最后两个项目在选中时显示正确的信息。
以下是编辑现有项目的代码:
Option Explicit
Private Sub cboOrderedFrom2_Change()
cboOrderedFrom2.BackColor = vbWhite
lblOrderedFrom2.ForeColor = vbBlack
End Sub
Private Sub cboOrderStatus2_Change()
cboOrderedFrom2.BackColor = vbWhite
lblOrderStatus2.ForeColor = vbBlack
End Sub
Private Sub cboItem_Change()
cboItem.BackColor = vbWhite
lblItemDescription2.ForeColor = vbBlack
Dim ws As Worksheet, i As Integer, wsLR As Variant
Set ws = ThisWorkbook.Sheets("Sheet1")
wsLR = ws.Cells(Rows.Count, 1).End(xlUp).Rows
For i = 3 To wsLR
If ws.Cells(i, 2) = Me.cboItem Then
Me.txtItemID.Value = ws.Cells(i, "A")
Me.txtPiecesIncluded2.Value = ws.Cells(i, "C")
Me.txtOrderDate2.Value = ws.Cells(i, "E")
Me.cboOrderStatus2.Value = ws.Cells(i, "G")
Me.txtQuantityOrdered2.Value = ws.Cells(i, "D")
Me.txtDateShipped2.Value = ws.Cells(i, "F")
Me.cboOrderedFrom2.Value = ws.Cells(i, "H")
Exit Sub
End If
Next i
End Sub
Private Sub cmdAddStore_Click()
frmAddStore.Show
End Sub
Private Sub cmdCancelEditOrRemoveItemDetails_Click()
Unload Me
End Sub
'Private Sub cmdRemoveItemDetails_Click()
'Dim ws As Worksheet, i As Integer, wsLR As Variant
'Set ws = ThisWorkbook.Sheets("Sheet1")
'wsLR = ws.Cells(Rows.Count, 1).End(xlUp).Rows
'For i = 3 To wsLR
'If ws.Cells(i, 2) = Me.cboItem Then
'Rows(i).EntireRow.Delete
'Sheet1.Activate
'Range("A1").End(xlDown).Offset(1, 0).Select
'ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + 1
'Unload Me
'End If
'Next i
'End Sub
Private Sub cmdSubmitEditItemDetails_Click()
If txtPiecesIncluded2.BackColor = vbRed Then
Exit Sub
End If
If txtQuantityOrdered2.BackColor = vbRed Then
Exit Sub
End If
If cboOrderStatus2.BackColor = vbRed Then
Exit Sub
End If
If cboOrderedFrom2.BackColor = vbRed Then
Exit Sub
End If
If cboItem.Value = "" Then
cboItem.BackColor = vbRed
lblItemDescription2.ForeColor = vbRed
Exit Sub
End If
If cboItem.BackColor = vbRed Then
Exit Sub
End If
Dim ws As Worksheet, i As Integer, wsLR As Variant
Set ws = ThisWorkbook.Sheets("Sheet1")
wsLR = ws.Cells(Rows.Count, 1).End(xlUp).Rows
For i = 3 To wsLR
If ws.Cells(i, "B") = Me.cboItem Then
ws.Cells(i, "A") = Me.txtItemID.Value
ws.Cells(i, "C") = Me.txtPiecesIncluded2.Value
ws.Cells(i, "E") = Me.txtOrderDate2.Value
ws.Cells(i, "G") = Me.cboOrderStatus2.Value
ws.Cells(i, "D") = Me.txtQuantityOrdered2.Value
ws.Cells(i, "F") = Me.txtDateShipped2.Value
ws.Cells(i, "H") = Me.cboOrderedFrom2.Value
Unload Me
Exit Sub
End If
Next i
End Sub
Private Sub spnPiecesIncluded2_Change()
txtPiecesIncluded2.Value = spnPiecesIncluded2.Value
End Sub
Private Sub spnQuantityOrdered2_Change()
txtQuantityOrdered2.Value = spnQuantityOrdered2.Value
End Sub
Private Sub txtPiecesIncluded2_Change()
If IsNumeric(txtPiecesIncluded2.Value) And txtPiecesIncluded2.Value >= spnPiecesIncluded2.Min And _
txtPiecesIncluded2.Value <= spnPiecesIncluded2.Max Then
spnPiecesIncluded2.Value = txtPiecesIncluded2.Value
txtPiecesIncluded2.BackColor = vbWhite
lblPiecesIncluded2.ForeColor = vbBlack
Else
txtPiecesIncluded2.BackColor = vbRed
lblPiecesIncluded2.ForeColor = vbRed
End If
End Sub
Private Sub txtQuantityOrdered2_Change()
If IsNumeric(txtQuantityOrdered2.Value) And txtQuantityOrdered2.Value >= spnQuantityOrdered2.Min And _
txtQuantityOrdered2.Value <= spnQuantityOrdered2.Max Then
spnQuantityOrdered2.Value = txtQuantityOrdered2.Value
txtQuantityOrdered2.BackColor = vbWhite
lblQuantityOrdered2.ForeColor = vbBlack
Else
txtQuantityOrdered2.BackColor = vbRed
lblQuantityOrdered2.ForeColor = vbRed
End If
End Sub
Private Sub UserForm_Click()
End Sub