Variant array in excel vba

Return to VBA Code Examples

In this Article

  • Array Variant Type
    • Variant Array Advantages
    • Declaring a Variant Array
    • Assign Values to Variant Array
    • Resize Variant Array
    • Variant Array Example

This tutorial will demonstrate how to create and work with Variant Arrays.

Array Variant Type

An array is a type of variable.  As with other variables, arrays are declared using the Dim Statement for a module or procedure level array, or the Public statement for a global level array.

A variant array is an array that is declared as having a variant data type. Variant data types can store any type of values: text, numbers, dates, time, or objects.

Variant Array Advantages

Variant arrays are special because they are easier to work with than typical arrays.

  1. They’re easier to declare.
  2. You don’t have to worry about what data type is being entered into the array (ex. if a cell contains text or numbers).
  3. You can populate the array with the Array function (if the Array size is not defined when declared).
  4. You don’t need to use the REDIM statement to resize / re-populate the array (if the Array size is not defined when declared).

Declaring a Variant Array

To declare a variant array, you can write this simple statement

Dim varNames()

This is considered a dynamic variant array.

Notice that you don’t need to specify the data type (it is assumed to be variant) or the array size.

Assign Values to Variant Array

Variant arrays can hold any type of values: text, numbers, dates, time, or objects.

To assign values to a variant array, you can use the Array Function:

'populate the array
    varNames() = Array("Fred", "Wilma", "Barney", "Betty")

This method may be easier to you than using the standard method:

'populate each element
   varNames(0) = "Fred"
   varNames(1) = "Wilma"
   varNames(2) = "Barney"
   varNames(3) = "Betty"

Resize Variant Array

Dynamic variant arrays can change size at runtime. You do not need to use the typical Redim or Redim Preserve statements required to resize standard arrays.

Variant Array Example

Putting all this together, this procedure will populate, and repopulate a variant array:

Sub TestArray()
'declare the  variable
    Dim varNames() As Variant
'populate the array
    varNames() = Array("Fred", "Wilma", "Barney", "Betty")
'return the values
    MsgBox Join(varNames, ",")
'repopulate the array
    varNames() = Array(400, 500)
'return the new values
    MsgBox Join(varNames, ",")
End Sub

We have populated the varNames array with 4 names, and then with 2 numbers.  The message boxes will appear as follows.

arr var msg 1

vba arr msg 2

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
vba save as

Learn More!

A question on variants. Im aware that variants in Excel vba are both the default data type and also inefficient (from the viewpoint of overuse in large apps). However, I regularly use them for storing data in arrays that have multiple data types. A current project I am working on is essentially a task that requires massive optimistaion of very poor code (c.7000 lines)- and it got me thinking; is there a way around this?

To explain; the code frequently stores data in array variables. So consider a dataset of 10 columns by 10000. The columns are multiple different data types (string, double, integers, dates,etc). Assuming I want to store these in an array, I would usually;

dim myDataSet(10,10000) as variant

But, my knowledge says that this will be really inefficient with the code evaluating each item to determine what data type it is (when in practise I know what Im expecting). Plus, I lose the control that dimensioning individual data types gives me. So, (assuming the first 6 are strings, the next 4 doubles for ease of explaining the point), I could;

dim myDSstrings(6,10000) as string
dim myDSdoubles(4,10000) as double

This gives me back the control and efficiency- but is also a bit clunky (in practise the types are mixed and different- and I end up having an odd number of elements in each one, and end up having to assign them individually in the code- rather than on mass). So, its a case of;

myDSstrings(1,r) = cells(r,1)
myDSdoubles(2,r) = cells(r,2)
myDSstrings(2,r) = cells(r,3)
myDSstrings(3,r) = cells(r,4)
myDSdoubles(3,r) = cells(r,5)
..etc...

Which is a lot more ugly than;

myDataSet(c,r) = cells(r,c)

So- it got me thinking- I must be missing something here. What is the optimal way for storing an array of different data types? Or, assuming there is no way of doing it- what would be best coding-practise for storing an array of mixed data-types?

Key Points

  • Think of an array in VBA array as a mini database to store and organized data (Example: student’s name, subject, and scores).
  • Before you use it, you need to declare an array; with its data type, and the number of values you want to store in it.

If you want to work with large data using VBA, then you need to understand arrays and how to use them in VBA codes, and in this guide, you will be exploring all the aspects of the array and we will also see some examples to use them.

What is an Array in VBA?

In VBA, an array is a variable that can store multiple values. You can access all the values from that array at once or you can also access a single value by specifying its index number which is the position of that value in the array. Imagine you have a date with student’s name, subject, and scores.

You can store all this information in an array, not just for one student but for hundreds. Here’s a simple example to explain an array.

In the above example, you have an array with ten elements (size of the array) and each element has a specific position (Index).

So, if you want to use an element that is in the eighth position you need to refer to that element using its index number.

The array that we have used in the above example is a single-dimension array. But ahead in this guide, we will learn about multidimensional arrays as well.

How to Declare an Array in VBA

As I mentioned above an array is the kind of variable, so you need to declare it using the keywords (Dim, Private, Public, and Static). Unlike a normal variable, when you declare an array you need to use a pair of parentheses after the array’s name.

Let’s say you want to declare an array that we have used in the above example.

Steps to declare an array.

Full Code

Sub vba_array_example()
Dim StudentsNames(10) As String
StudentsNames(0) = "Waylon"
StudentsNames(1) = "Morton"
StudentsNames(2) = "Rudolph"
StudentsNames(3) = "Georgene"
StudentsNames(4) = "Billi"
StudentsNames(5) = "Enid"
StudentsNames(6) = "Genevieve"
StudentsNames(7) = "Judi"
StudentsNames(8) = "Madaline"
StudentsNames(9) = "Elton"
End Sub

Quick Notes

  • In the above code, first, you have the Dim statement that defines the one-dimensional array which can store up to 10 elements and has a string data type.
  • After that, you have 10 lines of code that define the elements of an array from 0 to 9.

Array with a Variant Data Type

While declaring an array if you omit to specify the data type VBA will automatically use the variant data type, which causes slightly increased memory usage, and this increase in memory usage could slow the performance of the code.

So, it’s better to define a specific data type when you are declaring an array unless there is a need to use the variant data type.

Returning Information from an Array

As I mentioned earlier to get information from an array you can use the index number of the element to specify its position. For example, if you want to return the 8th item in the area that we have created in the earlier example, the code would be:

In the above code, you have entered the value in cell A1 by using item 8 from the array.

Use Option Base 1

I’m sure you have this question in your mind right now why we’re started our list of elements from zero instead of one?

Well, this is not a mistake.

When programming languages were first constructed some carelessness made this structure for listing elements in an array. In most programming languages, you can find the same structure of listing elements.

However, unlike most other computer languages, In VBA you can normalize the way the is index work which means you can make it begins with 1. The only thing you need to do is add an option-based statement at the start of the module before declaring an array.

Now this array will look something like the below:

Searching through an Array

When you store values in an array there could be a time when you need to search within an array.

In that case, you need to know the methods that you can use. Now, look at the below code that can help you to understand how to search for a value in an array.

Sub vba_array_search()

'this section declares an array and variables _
that you need to search within the array.
Dim myArray(10) As Integer
Dim i As Integer
Dim varUserNumber As Variant
Dim strMsg As String

'This part of the code adds 10 random numbers to _
the array and shows the result in the _
immediate window as well.
For i = 1 To 10
myArray(i) = Int(Rnd * 10)
Debug.Print myArray(i)
Next i

'it is an input box that asks you the number that you want to find.
Loopback:
varUserNumber = InputBox _
("Enter a number between 1 and 10 to search for:", _
"Linear Search Demonstrator")

'it's an IF statement that checks for the value that you _
have entered in the input box.
If varUserNumber = "" Then End
If Not IsNumeric(varUserNumber) Then GoTo Loopback
If varUserNumber < 1 Or varUserNumber > 10 Then GoTo Loopback

'message to show if the value doesn't found.
strMsg = "Your value, " & varUserNumber & _
", was not found in the array."

'loop through the array and match each value with the _
the value you have entered in the input box.
For i = 1 To UBound(myArray)
If myArray(i) = varUserNumber Then
strMsg = "Your value, " & varUserNumber & _
", was found at position " & i & " in the array."
Exit For
End If
Next i

'message box in the end
MsgBox strMsg, vbOKOnly + vbInformation, "Linear Search Result"

End Sub
  1. In the first part of the code, you have variables that you need to use in the code further.
  2. After that, the next part is to generate random numbers by using RND to get you 10 values for the array.
  3. Next, an input box to let enter the value that you want to search within the array.
  4. In this part, you have a code for the string to use in the message box if the value you have entered is not found.
  5. This part of the code uses a loop to loop through each item in the array and check if the value that you have entered is in the array or not.
  6. The last part of the code shows you a message about whether a value is found or not.

Introduction

One of the most common VBA techniques is the use of a For .. Next loop to iterate through all the cells in a range.  The syntax is straightforward and easily applied.  Unfortunately, for large data sets it can also be the programming equivalent of using a sledgehammer to crack a walnut — a lazy, inefficient and time-consuming approach for dealing with data.  

There are a number of superior techniques available in Excel VBA that will typically cut For .. Next loop run time by 95.0-99.9% such as:

Find Method (for finding and then working with specific data)

AutoFilter (applying a logical test to extract specific data)

SpecialCells (readymade collections of cells with certain characteristics, ie Blanks, Errors, Formulas, etc.)

Variant Arrays (for data manipulation on a large scale where every cell within the range of interest is to be processed)

 

Variant Arrays

This Article provides a brief overview of a code sample for using variant arrays with Excel VBA, in this case, removing all leading zeroes from a user selected range.  There are five basic steps in the attached code:

1) The user selects a range, which may be contiguous, or have multiple range areas.

2) If there is more than one cell in the range area then a variant array X() is used to read in the values from each range area.  The variant array is a 2-dimensional array containing the same amount of rows and columns as the parent range.

3) A For .. To loop using UBound for the array size limit is used to iterate through each element of the array (rows are looped first, followed by columns).

4) A simple Regular Expression replacement of any leading zeroes with vbNullString is performed.   Note that a For Next loop approach is suboptimal for variant arrays, see the Microsoft Support article:  
Using For Each to Loop Through Arrays Is Not Recommended.

5) The modified variant array is written back over the original range area.

Potential issues with using variant arrays notes:

See:
You may receive a «Run-time error 1004» error message when you programmatically set a large array string to a range in Excel 2003

I have experienced this when modifying very long formulae with arrays.  The workaround was to move any such strings into a second array, and then write this second array back to the spreadsheet, cell by cell. All other strings are dealt with as per the code below, the array is written back to the range in a single stage.

Other notes:

It is possible to return cell formulas (in A1 or R1C1 notation respectively) rather than cell values by using

X = rng1.Formula

or

X = rng1.FormulaR1C1

My Mappit! addin article uses the later approach to quickly determine potential spreadsheet errors  
Mappit! — a free Excel model auditing addin

Readers of this article will find Aikimark’s
Fast Data Push to Excel case study article very interesting reading.

For more information on using Regular Expressions in VBA please see Patrick Matthew’s excellent article,  
Using Regular Expressions in Visual Basic

=-=-=-=-=-=-=-=-=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=

If you liked this article and want to see more from this author,
please click here.

If you found this article helpful, please click the Yes button near the:

      Was this article helpful?

label that is just below and to the right of this text.   Thanks!

=-=-=-=-=-=-=-=-=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=

'Press Alt + F11 to open the Visual Basic Editor (VBE)
                      'From the Menu, choose Insert-Module.
                      'Paste the code into the right-hand code window.
                      'Press Alt + F11 to close the VBE
                      'In Xl2003 Goto Tools … Macro … Macros and double-click KillLeadingZeros
                      
                      Sub KillLeadingZeros()
                          Dim rng1 As Range
                          Dim rngArea As Range
                          Dim lngRow As Long
                          Dim lngCol As Long
                          Dim lngCalc As Long
                          Dim objReg As Object
                          Dim X()
                      
                        
                          On Error Resume Next
                          Set rng1 = Application.InputBox("Select range for the replacement of leading zeros", "User select", Selection.Address, , , , , 8)
                          If rng1 Is Nothing Then Exit Sub
                          On Error GoTo 0
                      
                          'See Patrick Matthews excellent article on using Regular Expressions with VBA
                          Set objReg = CreateObject("vbscript.regexp")
                          objReg.Pattern = "^0+"
                      
                         'Speed up the code by turning off screenupdating and setting calculation to manual
                         'Disable any code events that may occur when writing to cells
                          With Application
                              lngCalc = .Calculation
                              .ScreenUpdating = False
                              .Calculation = xlCalculationManual
                              .EnableEvents = False
                          End With
                      
                          'Test each area in the user selected range
                          
                          'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on
                          For Each rngArea In rng1.Areas
                              'The most common outcome is used for the True outcome to optimise code speed
                              If rngArea.Cells.Count > 1 Then
                                 'If there is more than once cell then set the variant array to the dimensions of the range area
                                 'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks    
                                  X = rngArea.Value2
                                  For lngRow = 1 To rngArea.Rows.Count
                                      For lngCol = 1 To rngArea.Columns.Count
                                          'replace the leading zeroes
                                          X(lngRow, lngCol) = objReg.Replace(X(lngRow, lngCol), vbNullString)
                                      Next lngCol
                                  Next lngRow
                                  'Dump the updated array sans leading zeroes back over the initial range
                                  rngArea.Value2 = X
                              Else
                                  'caters for a single cell range area. No variant array required
                                  rngArea.Value = objReg.Replace(rngArea.Value, vbNullString)
                              End If
                          Next rngArea
                      
                          'cleanup the Application settings
                          With Application
                              .ScreenUpdating = True
                              .Calculation = lngCalc
                              .EnableEvents = True
                          End With
                      
                          Set objReg = Nothing
                      End Sub
                      

Open in new window

Содержание:

  • Зачем нужны массивы

  • Что такое массив

  • Типы массивов

  • Объявление массивов

  •     Объявление фиксированных массивов
  •     Объявление динамических массивов
  • Изменение элементов массива

  • Чтение элементов массива

  • Определение границ массива

  • Перебор элементов массива

  • Передача массивов в подпрограммы

  • Массив с элементами типа массив

  • Функция Array

  • Функция Split

  • Нюансы работы с динамическими массивами

  •     Неинициализированный массив
  •     Расширение массива
  •     Удаление массива
  • Получение массива на основе диапазона Excel

  • Дополнительные источники

Зачем нужны массивы

Массивы очень упрощают процесс программирования. Без них практически невозможно написать универсальную программу. Например, представьте себе, что вы манипулируете информацией о квартирах жилого дома. Вы объявляете переменные K1 — для первой квартиры, K2 — для второй и так далее. K1=54 будет означать, что площадь первой квартиры 54 кв.м., К2=72 и т.д. Теперь представим, что нам надо подсчитать общую площадь всех квартир в доме. Очевидно, что это что-то типа Total_S = K1+K2+…+Kn. В одном доме у нас 36 квартир, а в другом 144. Представляете бредовость процесса подобного программирования? Если в первом случае я должен буду использовать 36 отдельных переменных для вычисления общей площади, то для второго дома уже 144. Очень быстро вы придёте к мысли, что вам нужна переменная, состоящая из нумерованных ячеек. Тогда обретают смысл все те операторы циклов, входящие в состав любого языка программирования. Но об этом чуть позже…

▲ вверх

Что такое массив

Массив — переменная, состоящая из некоторого количества однотипных элементов. У массива, как и у любой другой переменной, есть имя. А доступ к конкретному элементу массива осуществляется через указание в скобках после имени его индекса. Например, A(5) означает, что я обращаюсь к элементу с индексом 5 массива, имеющего имя A.

▲ вверх

Типы массивов

Массивы в VBA и во многих других языках программирования делятся на 2 класса:

  • Фиксированные массивы. Такие массивы состоят из заранее известного количества элементов. Это количество определяется во время объявления массива и уже не может быть изменено в процессе его жизненного цикла. Вы, конечно же, сможете использовать меньшее количество элементов, но не существует способа увеличить количество элементов сверх объявленного.

  • Динамические массивы. Эти массивы можно «переобъявлять» в процессе жизненного цикла. То есть мы можем управлять количеством элементов динамических масивов в зависимости от наших потребностей. Это очень удобно, так как в подавляющем большинстве случаев программист не может заранее знать, с каким объёмом данных он столкнётся. Если вы собираетесь писать более-менее универсальные программы, то этот тип массивов определенно стоит изучить.

▲ вверх

Объявление массивов

Объявление фиксированных массивов

Рекомендация: при объявлении массивов VBA я советую вам давать всем именам префикс «arr«. Я сторонник венгерской нотации.

Как мы видим, тут объявлено 2 одномерных массива arrTemp и arrTest. Одномерные массивы в программировании также часто называют векторами. Типом элементов первого массива является Long, второго массива — String. В этом типе синтаксиса в скобках указан максимальный индекс (верхняя граница ) элемента массива. А что насчёт минимального индекса (нижней границы) массива? По-умолчанию минимальным индексом является ноль. В данном случае стандартное поведение интерпретатора языка VBA можно изменить при помощи оператора option base {0|1}. Option base 1 заставляет VBA считать нижней границей массива — единицу, а не ноль.

Таким образом, по-умолчанию массив arrTemp имеет 11 элементов — от 0 до 10. Но, если в начало модуля, в котором этот массив объявляется, вставить оператор Option Base 1, то массив arrTemp будет иметь 10 элементов — от 1 до 10.

Помимо вышеуказанного вы вправе использовать следующий синтаксис, который НЕ зависит от option base {0|1}:

таким образом вы в явном виде указываете и нижнюю, и верхнюю границы. Как видите, нижняя граница совершенно не обязательно должна начинаться с 0 или 1. Более того, индексы границ могут принимать и отрицательные значения, главное чтобы нижняя была меньше верхней.

Помимо одномерных массивов, можно объявлять и массивы с размерностью больше единицы.

arrMulti — двумерный массив, а arrData3 — трёхмерный. Первый содержит 11*31=341 элемент, второй — 2*3*10=60 элементов. Теоретически допускается объявлять до 60 размерностей массива.

Какие типы данных могут стать элементами массива? Тут всё, как в шутке про фамилию еврея, — абсолютно любой тип данных годится на роль элемента массива, включая объектные типы, User Data Type, другие массивы (через тип Variant). Если вы не указываете при объявлении тип данных массива, то предполагается, что этим типом является тип Variant.

▲ вверх

Объявление динамических массивов

Динамические массивы объявляться так:

Однако, использовать их после такого объявления пока ещё нельзя. Необходимо выделить память под массив. Особенность работы с динамическим массивом как раз состоит в том, что программист отвечает за его своевременное расширение (усечение) в памяти. Для этого существует специальный оператор, который имеет следующий синтаксис:

    ReDim [Preserve] varname(subscripts) [As Type]

Например:

После этого оператора, вы можете использовать элементы массива arrOpen с 0-го по 5-й. Всё, что мы говорили про оператор option base и нижнюю границу, верно и для динамических массивов. Предположим, что вы сохранили информацию в элементах 0-5 и у вас поспела новая порция информации для элементов 6-11. Чтобы разместить в данном массиве новые элементы и не потерять старые, вы должны сделать следующее:

то есть мы тут увеличиваем верхнюю границу массива и используем ключевое слово Preserve, чтобы во время этой операции не потерять текущее содержимое arrOpen, так как в противном случае (без слова Preserve) массив будет расширен, а память заполнена нулями. Вы также вправе вообще не декларировать массив оператором Dim, а сделать это впервые через ReDim и там же указать лип элементов. Но, если вы в первом ReDim (или Dim) указали определенный тип элементов, то в последующих операторах ReDim этот тип переопределён быть не может — возникнет ошибка на этапе компиляции проекта.

▲ вверх

Изменение элементов массива

Пора бы нам уже научиться пользоваться нашими массивами — то есть записывать информацию в их элементы и считывать её оттуда. Это довольно просто:

Как и с обычными переменными запись информации в элемент массива происходит через оператор присваивания (=), но указанием индекса элемента массива.

▲ вверх

Чтение элементов массива

▲ вверх

Определение границ массива

В подпрограммах часто приходится иметь дело с массивами, которые переданы вам в качестве параметра (как это сделать показано ниже), поэтому в этом случае очень актуален вопрос определения нижней и верхней границ индекса массива. Для этого в языке предусмотрены 2 функции: LBound и UBound. Первая возвращает нижнюю границу индекса, вторая — верхнюю.

    LBound( array [, dimension])

    UBound( array [, dimension])

Для одномерных массивов параметр dimension можно не указывать. Для многомерных массивов его указывать необходимо. Кстати, это означает, что, если вы точно не знаете, с каким массивом имеете дело, но необходимо узнать его первую размерность, то лучше использовать вариант UBound(arrTemp,1), а не UBound(arrTemp), так как последний вариант вызовет ошибку, если массив окажется многомерным.

Если вы ошибётесь с указанием правильного индекса массива, то возникнет ошибка периода исполнения с кодом 9. Эта же ошибка возникнет, если вы в функции LBound / UBound укажете несуществующую размерность массива (например, 3 для двумерного массива).

▲ вверх

Перебор элементов массива

Собственно массивы нужны для того, чтобы хранить в них однотипную информацию и перебирать их в цикле. Как правило, алгоритм делает что-то полезное с одним элементом массива, а цикл повторяет эти типовые действия для всех элементов массива.

Наиболее удобный оператор цикла для перебора элементов массива — это безусловно ForNext.

так же есть способ не заботиться об определении нижней и верхней границ, если алгоритм не требует от нас знания текущего индекса массива:

Вы, конечно, можете перебирать массив и в других типах циклов DoLoop, но, право, смысла и удобства в этом не много. По крайней мере я не сталкивался, кажется, с ситуациями, когда для перебора массива цикл For не подошёл.

▲ вверх

Передача массивов в подпрограммы

Массивы удобнее всего передавать в подпрограммы в виде параметра типа Variant.

Обратите внимание, что функции GetResult в качестве параметра передаются массивы. При чём, в первом случае это массив с типом элементов Long, а во втором — String. За счёт того, что внутри функции используются переменные типа Variant, то сначала функция нам возвращает сумму элементов массива arrIntegers, а во втором результат сложения (конкатенации) строк массива arrStrings. Кроме того, параметр parArray не описан как массив (parArray As Variant), но мы внутри функции GetResult ведём себя с ним, как с массивом (For Each Element In parArray)! Это возможно, так как переменные типа Variant умеют определять, что им присваивается и вести себя далее в соответствии с тем, что они содержат. Если переменной parArray присвоили массив (через вызов функции — строки 17 и 18), то она себя будет вести как массив.

▲ вверх

Массив с элементами типа массив

Продемонстрируем, как можно хранить в качестве элементов массива другие массивы.

Результат отладочной печати:

▲ вверх

Функция Array

Данная функция полезна для создания справочных массивов. Она возвращает переменную типа Variant, содержащую одномерный массив с типом элементов Variant.

    Array( arglist )

Вызов функции без параметров приведёт к возврату массива нулевой длинны. При этом будет наблюдаться интересный эффект LBound вернёт вам 0, а UBound вернёт -1, то есть верхняя граница окажется меньше нижней границы.

▲ вверх

Функция Split

Split возвращает одномерный массив, содержащий подстроки, из строкового выражении с учётом указанного разделителя

    Split(expression[, delimiter[, limit[, compare]]])

  • expression — строковое выражение, содержащая подстроки и разделители. Обязательный параметр.

  • delimiter — текстовый разделитель. Необязательный параметр. Если опущен, то предполагается, что разделителем является символ пробела.

  • limit — количество подстрок, которое необходимо вернуть. -1 или отсутствие параметра означает, что вернуть надо все подстроки.

  • compare — константа, указывающая тип сравнения для символов разделителей. 1 — текстовое сравнение (без учёта регистра), 0 — бинарное сравнение (с учётом регистра).

Результат выглядит так:

Если вы в качестве разделителя укажете пустую строку, то на выходе получите массив, состоящий из одного элемента. Кстати, split всегда возвращает массив с нулевой нижней границей вне всякой зависимости от наличия option base 1.

▲ вверх

Нюансы работы с динамическими массивами

Неинициализированный массив

У динамического массива есть такое промежуточное состояние, когда он уже объявлен, но ещё не содержит никаких элементов.

То есть у переменной динамического массива есть такое состояние, когда мы не можем воспользоваться вспомогательными функциями LBound / UBound для определения его (массива) статуса. Это особенно надо учитывать, когда вы пишите подпрограммы, работающие с массивами. Прежде чем работать (перебирать) массив необходимо убедиться, что он проинициализирован, в противном случае программа вылетит с ошибкой 9.

Для этого я предлагаю пользоваться функцией подобной нижеописанной IsNotEmptyArray:

▲ вверх

Расширение массива

Как правило, расширять динамический массив приходится в цикле. Возможны 2 стратегии: расширение на 1 элемент, как только в этом есть необходимость (назовём это эластичным расширением), и расширение авансом, когда вы увеличиваете верхнюю границу скачками, скажем сразу на 100 элементов. Реализовав оба варианта, я для себя сделал вывод, что авансовое расширение получилось и компактнее, и работает быстрее, так как операция расширения, вообще говоря, затратна и, чем реже вызывается, тем лучше.

Результат:

Авансовый метод вышел даже компактнее

▲ вверх

Удаление массива

Существует оператор Erase, который полностью освобождает память из-под динамического массива. Будучи вызванным для статического массива он его обнуляет, а если он строковый, то элементам присваивается пустые строки.

▲ вверх

Самый эффективный по скорости способ получить содержимое диапазона Excel для манипулирования в VBA — это скопировать его в массив с элементами Variant. Делается так:

Даже, если вы передаёте в массив столбец или строку, получаемый массив всегда будет иметь 2 измерения. Измерение 1 отвечает за строки, измерение 2 — за столбцы. То есть ячейка C5 будет в элементе arrTemp(5,3). Нижняя граница таких массивов всегда будет начинаться с единицы.

▲ вверх

Дополнительные источники

В качестве источника дополнительной информации по массивам я могу порекомендовать замечательный, исчерпывающий ресурс Чарльза Пирсона (Charles H. Pearson). Его сайт следует штудировать всем, кто серьёзно осваивает VBA. Конкретно по массивам там огромное количество готовых подпрограмм для работы с ними, исходные коды, снабженные подробнейшими комментариями, продвинутые объяснения для копающих в глубину. Без преувеличения великолепный ресурс!

▲ вверх

Читайте также:

  • Работа с объектом Range

  • Поиск границ текущей области

  • Структуры данных и их эффективность

  • Автоматическое скрытие/показ столбцов и строк

Понравилась статья? Поделить с друзьями:
  • Variant 1 choose the right word
  • Variance function in excel
  • Variance formula in excel
  • Variable field in word
  • Var excel что это