当我尝试编辑现有项目的详细信息时,我的代码无法填写列表中最后两项的相应信息。有一个组合框用于选择要编辑的项目,然后是项目ID的文本框,以及项目订购日期和发货时间的文本框,然后还有两个组合框用于选择运输状态和在线商店它是通过购买的。这些字段都自动填充了所选项目的相应信息。可以编辑与所选项目对应的详细信息,但项目名称本身和项目ID除外。它适用于除最后2项之外的所有项目,我不知道为什么。以下是编辑现有项目的代码:
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 cboRemoveOrEditItemDetails_Change()
cboRemoveOrEditItemDetails.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.cboRemoveOrEditItemDetails 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.cboRemoveOrEditItemDetails 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 cboRemoveOrEditItemDetails.Value = "" Then
cboRemoveOrEditItemDetails.BackColor = vbRed
lblItemDescription2.ForeColor = vbRed
Exit Sub
End If
If cboRemoveOrEditItemDetails.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.cboRemoveOrEditItemDetails 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
答案 0 :(得分:2)
填充ComboxBox
非常简单。 .List
属性可以接受数组,也可以使用.AddItem
方法单独添加项目。
我从你的代码中注意到你正在一次读取每个单元格到你的数组。你是否意识到你可以一次完成这一切?假设您定义所需范围的代码如下所示:
Dim lastRow As Long, lastCol As Long
Dim readRange As Range
'Define the range to be read
With Sheet1
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
lastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
Set readRange = .Range(.Cells(3, "A"), .Cells(lastRow, lastCol))
End With
然后填充数组的代码可能只是一行:
Dim data As Variant
'Read range to array
data = readRange.Value2
同样适用于将数组写入Worksheet
:
Dim writeRange As Range
'Write the data
Set writeRange = Sheet2.Range("A1").Resize(UBound(data, 1), UBound(data, 2))
writeRange.Value = data
以下是使用全部或部分数组填充ComboBox
的三个示例:
'Populate the combobox
UserForm1.ComboBox1.List = data
'Or, if you want more than one column in combobox
With UserForm1.ComboBox2
.ColumnCount = UBound(data, 2)
.List = data
End With
'Or, if you want a specific index (not the first) from your array
Dim r As Long, index As Long
index = 2
For r = 1 To UBound(data, 1)
UserForm1.ComboBox3.AddItem data(r, index)
Next