如何用现有的数组填充combobox

当我尝试编辑现有项目的详细信息时,我的代码未能填写列表中最后两项的相应信息。 有一个combobox,用于select要编辑的项目,然后是项目ID的文本框,以及该项目的订购date和发货时间的文本框,然后两个combobox用于select发货状态和在线商店它是通过购买。 这些字段都会自动填充所选项目的相应信息。 除了项目名称本身和项目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 

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