在一个MsgBox VBA中显示所有数组元素值

请忍受我的代码。 (我不是一个好的编码器,不熟悉所有的VBA语法。)

我正在为我们所有的家庭书籍创build一个数据库。

我没有使用ACCESS或SQL,只是简单地将所有的UserForminput数据logging到一个Excel工作表中。

在我的用户窗体中,所有具有如下类别的数据:作者,stream派,出版者,书籍在内的位置等,都是通过ComboBoxinput的。

每个ComboBox的初始RowSource是Excel表中的一个范围。 在这个范围内,我已经为每个类别input了一些项目。 因此,在执行macros时,单击每个combobox的下拉箭头时,将显示列表项。

下面的代码中“Private Sub CmdEditList_Click()”的function是首先更新每个类别中的项目列表,如果在现有列表中没有find每个ComboBox中的数据的话。 其次,更新每个ComboBox的RowSource。

下面的MsgBox代码行的目的,我有一个问题,是通知用户哪些类别有一个项目添加到它的列表。

MsgBox "The following Categories were updated:" & vbNewLine & msg` 

但是,例如,更新3个类别(作者,发布者和系列)的情况下,不显示作者和发布者,而是在2个换行符之后仅显示“系列”。

问题的原因是什么? 解决办法是什么?

  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 

 ReDim MsgBoxArr(UpdateItemCnt) 

应该

 ReDim Preserve MsgBoxArr(UpdateItemCnt) 

如果您在不Preserve情况下调整数组大小,则任何现有的内容都将丢失