2017-05-21 12 views
1

私のコードを付けてください。 (私は良いコーダーではなく、すべてのVBA構文に精通していません)1つのMsgBox VBAにすべての配列要素の値を表示

私はすべての家庭用書籍のデータベースを作成しています。

Excelシートにすべてのユーザーフォーム入力データを記録するだけで、ACCESSまたはSQLを使用していません。

私のユーザーフォームでは、Author、Genre、Publisher、本棚の場所などのカテゴリを持つすべてのデータがComboBoxを介して入力されます。

各ComboBoxの最初のRowSourceは、Excelシートの範囲です。この範囲では、すでに各カテゴリの項目を入力しています。したがって、マクロを実行すると、各ComboBoxのドロップダウン矢印がクリックされると、リストアイテムが表示されます。

以下のコードの "Private Sub CmdEditList_Click()"の機能は、各コンボボックスのデータが既存のリストに見つからない場合は、まず各カテゴリのアイテムのリストを更新することです。次に、各ComboBoxのRowSourceを更新します。

私は問題を抱えている以下のMsgBoxコード行の目的は、カテゴリのそれに追加したアイテムを持っているユーザーに知らせるためであるlist.`

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

ですが、例えば例で3つのカテゴリ(Author、Publisher & Series)が更新され、2つの改行の後にAuthorとPublisherが表示されず、 "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 
+0

を失っている

ReDim Preserve MsgBoxArr(UpdateItemCnt) 

である必要があり、それは複数選択コンボボックスですか? – PeterT

+0

いいえ、私はマルチセレクトをサポートしています。 – Karen88

答えて

0
ReDim MsgBoxArr(UpdateItemCnt) 

あなたはPreserveせずに、配列のサイズを変更するならば、既存のすべてのコンテンツが

+0

ありがとうございます。それは私の問題を解決しました。 =) – Karen88

関連する問題