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

Display All Array Element Values in One MsgBox VBA

请耐心等待我的代码。 (我不是一个好的编码员,也不熟悉所有 VBA 语法。)

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

我没有使用 ACCESS 或 SQL,只是简单地将所有用户窗体输入数据记录到 Excel Sheet.

在我的用户窗体中,所有数据都通过组合框输入,类别如下:作者、流派、出版商、书在家中的位置等。

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

下面代码中"Private Sub CmdEditList_Click()"的作用是,如果在现有列表中找不到每个ComboBox中的数据,则首先更新每个类别中的项目列表。其次,更新每个 ComboBox 的 RowSource。

下面的 MsgBox 代码行的目的是通知用户哪个类别的列表中添加了项目。`

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

但是在更新 3 个类别(作者、出版商和系列)的情况下,不会显示作者和出版商,而是在 2 个换行符之后,仅显示 "Series"。

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

    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 的情况下调整数组大小,则任何现有内容都会丢失