Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to declare an array in vba
How to declare an array in vba
1, vba declares an array with Dim keyword, followed by the array name, followed by parentheses, and then the parentheses can indicate the length of the array, or you can use the to keyword to indicate the range, and finally use the as keyword to indicate the type. See the figure below for the specific format:

Let's use an example to illustrate the usage. First of all, there are 6000 values in the table, and the values greater than 90 are filtered out and copied to column B or column C, and the code is written in two ways, one method does not contain an array, and the other method contains an array. Compare the speed of the two methods:

2. In the Excel worksheet, click the menu bar development tool, and in its toolbar, click visual basic:

3. At this time, you will enter the VBA editing interface, click Insert in the menu bar, and select a module from the drop-down menu:

4. At this time, the software will pop up the module editing window, where you can enter vba code:

5. Enter two codes with and without arrays in the code editing window. Here you can see that an array of 6000 elements is declared with the Dim keyword:

6. In the development tool menu of the worksheet interface, click the Insert button to insert the form control, which is associated with two sets of codes respectively:

7. Click the filter data 1 button, that is, the array is not included, and the running time of the program is 13.438+09 seconds:

8. Click the "Filter Data 2" button, which is the group containing the array. The running time of the program is 0.03 13 seconds. You can see that this method is much faster.