Excel VBA using an array to speed up code

时间:2018-08-22 13:56:38

标签: arrays excel vba loops

I am trying to create an array, store values in the array and then write the values of the array to a spreadsheet in VBA. This codes takes 1+ hour to run on my computer and I think that an array could really speed up the code.

However, I need help with creating the array, populating the array from the comboboxes and finally write the values of the array to the worksheet.

  1. Create an n-dimensional array
  2. Fill the n-dimensional array with the values of the ComboBoxes.
  3. Iterate through all ComboBoxes.
  4. Store values in the array
  5. Write values from the array to the spreadsheet

    Sub WantToUseArray()

    Dim k As Integer
    Dim l As Integer
    
    Sheets("Test").ComboBox1.ListIndex = 0
    
    For l = 0 To 25
    Sheets("Test").ComboBox3.ListIndex = l
    
    Sheets("Test").ComboBox2.ListIndex = 0
    
    For n = 0 To 25
    Sheets("Test").ComboBox4.ListIndex = n
    
    Sheets("Points").Select
    
    Dim LR As Long
    LR = Cells(Rows.Count, 1).End(xlUp).Row + 1
    
    Cells(LR, "A").Value = Sheets("Test").Range("G5").Value
    Cells(LR, "B").Value = Sheets("Test").Range("G6").Value
    
    Cells(LR, "C").Value = Sheets("Test").Range("O5").Value
    Cells(LR, "D").Value = Sheets("Test").Range("O6").Value
    
    Cells(LR, "E").Value = Sheets("Test").Range("X5").Value
    Cells(LR, "F").Value = Sheets("Test").Range("X6").Value
    
    Cells(LR, "G").Value = Sheets("Test").Range("G6").Value + Sheets("Test").Range("X6").Value
    
    Cells(LR, "H").Value = Sheets("Test").Range("X6").Value + Sheets("Test").Range("G6").Value
    
    Cells(LR, "I").Value = Sheets("Test").Range("K40").Value
    Cells(LR, "J").Value = Sheets("Test").Range("K41").Value
    
    Cells(LR, "K").Value = Sheets("Test").Range("K51").Value
    Cells(LR, "L").Value = Sheets("Test").Range("K52").Value
    
    
    
    Next
    Next
    
    End Sub
    

1 个答案:

答案 0 :(得分:2)

此代码遍历给定$comment->commenter->name; // Name of the person who commented $comment->user->name; // Name of the person who was commented on 中的每个combobox,生成一个worksheet,其中包含每个array列表的列表值,然后将所有内容打印到第一列。 myArray只有一个维度。其内容是其他comobox。如果arrays具有不同的列表长度,则会创建一个comoboxes

要帮助可视化jagged array,请通过在菜单栏中查看然后选择arrays来启用Locals Window。参见代码下方的图片。

Locals Window

enter image description here