请耐心等待我的代码。 (我不是一个好的编码器,也不熟悉所有的VBA语法。)
我为所有家庭书籍创建了一个数据库。
我没有使用ACCESS或SQL,只是将所有UserForm输入数据记录到Excel工作表中。
在我的UserForm中,所有数据都具有类别:作者,流派,发布者,内部书籍的位置等,通过ComboBox输入。
每个ComboBox的初始RowSource是Excel工作表中的范围。在此范围内,我已经为每个类别输入了一些项目。因此,在执行宏时,单击每个ComboBox的下拉箭头时,将显示列表项。
" Private Sub CmdEditList_Click()"的功能在下面的代码中,首先,如果在现有列表中找不到每个ComboBox中的数据,则更新每个类别中的项目列表。第二,更新每个ComboBox的RowSource。
下面的MsgBox代码行的目的是告诉用户哪些类别的项目已添加到其列表中。
MsgBox "The following Categories were updated:" & vbNewLine & msg`
但是在例如更新3个类别(作者,发布者和系列)的情况下,不显示作者和发布者,而是在2个换行之后,仅#34;系列"显示。
问题的原因是什么?解决方案是什么?
Private Sub CmdEditList_Click()
Dim NextListRow As Long
Dim ComboArr()
Dim RangeArr()
Dim MsgBoxArr()
Dim CategoryArr()
Dim i As Integer
Dim UpdateItemCnt As Integer
Dim mbi As Integer
Const LASTINDEX = 8
i = 0
UpdateItemCnt = -1
ComboArr = Array(ComboAuthor, ComboGenre, ComboPublisher, _
ComboLocation, ComboSeries, ComboPropertyOf, _
ComboRating, ComboRatedBy, ComboStatus)
RangeArr = Array("R", "S", "T", "U", "V", "W", "X", "Y", "Z")
CategoryArr = Array("Author", "Genre", "Publisher", "Location", "Series", _
"Property Of", "Rating", "Rated By", "Status")
Do While i <= LASTINDEX
'Checks each Combobox, if ther's a data input.
If Len(Trim(ComboArr(i).Value)) <> 0 Then
Set wkb = ThisWorkbook
wkb.Sheets("Database").Activate
With ActiveSheet
'Finds the cell, where a new item of a Category can be placed in the excel sheet.
NextListRow = .Cells(.Rows.Count, RangeArr(i)).End(xlUp).Row + 1
End With
'Check if the entered data is not in the existing list.
'If True, ComboBox data is in the list.
If Application.CountIf(Range(RangeArr(i) & "2" & ":" & RangeArr(i) & NextListRow), _
ComboArr(i).Value) > 0 Then
GoTo NextRoutine
Else
UpdateItemCnt = UpdateItemCnt + 1
ReDim MsgBoxArr(UpdateItemCnt)
MsgBoxArr(UpdateItemCnt) = CategoryArr(i)
MsgBox MsgBoxArr(0) 'To Check the value of MsgBoxArr(0) after 2nd assignment.
'Upon checking via debug simulation, the value = "".
'Assigns the ComboBox Value under its corresponding Category in excel sheet.
Database.Cells(NextListRow, RangeArr(i)).Value = ComboArr(i).Value
'Refreshes the range of the list to be displayed via the ComBox dropdown arrow.
Range(RangeArr(i) & "2", Range(RangeArr(i) & Rows.Count).End(xlUp)).Name = "Dynamic"
ComboArr(i).RowSource = "Dynamic"
End If
NextRoutine:
Else
GoTo EndRoutine
EndRoutine:
End If
i = i + 1
Loop
MsgBox MsgBoxArr(0) 'To Check the value of MsgBoxArr(0) after loop.
'Upon checking via debug simulation, the value = "".
For mbi = LBound(MsgBoxArr) To UBound(MsgBoxArr)
msg = msg & MsgBoxArr(mbi) & vbNewLine
Next mbi
MsgBox "The following Categories were updated:" & vbNewLine & msg
'In cases, wherein new item for Author, Publisher and Series were input in the UserForm,
'the MsgBox only shows: The following Categories were updated:
'
'
' Series
End Sub
答案 0 :(得分:1)
ReDim MsgBoxArr(UpdateItemCnt)
应该是
ReDim Preserve MsgBoxArr(UpdateItemCnt)
如果您在没有Preserve
的情况下调整数组大小,那么任何现有内容都将丢失