Запись диапазона в массив vba excel

Копирование значений из диапазона ячеек в массив и обратно с помощью VBA Excel. Простейшие примеры обмена значениями между диапазоном и массивом.

Как известно, VBA обрабатывает информацию в массивах значительно быстрее, чем в ячейках рабочего листа Excel. Поэтому, при работе с большими объемами данных, удобнее использовать массивы, чем наблюдать во время выполнения кода за мерцанием изображения на экране или просто смотреть в неизменную картинку, если обновление экрана отключено (Application.ScreenUpdating = False). Здесь обмен значениями между массивом и диапазоном ячеек будет вполне уместен.

Копирование значений из диапазона ячеек в массив

Чтобы скопировать значения из диапазона ячеек в массив, необходимо объявить переменную универсального типа (As Variant) и присвоить ей значения диапазона ячеек с помощью оператора присваивания (=):

Dim a As Variant

a = Range(«A1:C3»)

VBA Excel автоматически преобразует объявленную переменную в двумерный массив, соответствующий размерности диапазона ячеек, в нашем случае в массив — a(1 To 3, 1 To 3), и заполняет его значениями. Нумерация измерений массивов, созданных таким образом, начинается с единицы (1).

Можно, в этом случае, объявить сразу динамический массив, чтобы изначально указать, что эта переменная будет массивом. Так как свойством диапазона ячеек по умолчанию в VBA Excel является значение (Value), его можно в коде явно не указывать, но, при желании, можно и указать. Получится такая конструкция, аналогичная первой:

Dim a() As Variant

a = Range(«A1:C3»).Value

Стоит отметить, что для копирования значений из диапазона ячеек в массив можно использовать только обычную переменную или динамический массив универсального типа (Variant). VBA Excel автоматически преобразовывает их в двумерный массив. Если объявить двумерный массив с указанной заранее размерностью, использовать его не получится, будет сгенерирована ошибка с сообщением: Can’t assign to array (Нельзя назначать массив).

Копирование значений из массива в диапазон ячеек

Значения в диапазон ячеек добавляются из массива с помощью оператора присваивания (=):

Range(«A6:F15») = a

‘или

Range(«A6:F15»).Value = a

‘где a — переменная двумерного массива

Обратите внимание, что вставить значения в диапазон ячеек можно только из двумерного массива. Размерность такого массива может начинаться с нуля (0). Количество элементов в измерениях массива должно совпадать с количеством строк и столбцов в диапазоне ячеек. Если вам нужно вставить значения в одну строку или в один столбец, укажите размерность единственной строки или единственного столбца как (0) или (1 To 1), если вы хотите использовать нумерацию измерений своего массива с единицы. Например, для записи десяти значений из массива в одну строку можно объявить такой массив — massiv(9, 0), или в один столбец — massiv(0, 9).

Для вставки значений в диапазон ячеек из массива идеально подойдет массив, созданный для копирования в него значений из диапазона. В этом случае, данные с рабочего листа Excel переносятся в массив, обрабатываются и, после обработки, вставляются обратно в ту же или другую таблицу на том же или другом рабочем листе.

Обмен значениями между двумя диапазонами

Обмен значениями можно осуществить в VBA Excel не только между массивом и диапазоном, но и между двумя диапазонами одинаковой размерности:

Range(«B2:D6») = Range(«G7:I11»).Value

У диапазона, являющегося источником значений, обязательно должно быть указано свойство Value.

Если диапазон ячеек, принимающий значения, по размеру меньше диапазона-источника, то он будет заполнен полностью:

Range(«B2:D6») = Range(«G5:L13»).Value

Если принимающий диапазон ячеек по размеру больше передающего, то часть его будет заполнена значениями диапазона-источника, а остальные ячейки — значениями #Н/Д:

Range(«B2:D6») = Range(«G7:H9»).Value

Простейшие примеры обмена значениями

Эти примеры составлены так, чтобы вам не пришлось совершать лишних действий, просто скопируйте их в свой модуль любой книги Excel с поддержкой макросов и запустите по очереди на выполнение.

Пример 1

Заполнение двумерного массива значениями и и их присвоение диапазону ячеек на рабочем листе Excel:

Sub Test1()

Dim a(2, 2) As Variant

a(0, 0) = «телепузик»

a(0, 1) = «журналист»

a(0, 2) = «ящерица»

a(1, 0) = «короед»

a(1, 1) = «утенок»

a(1, 2) = «шмель»

a(2, 0) = 200

a(2, 1) = 300

a(2, 2) = 400

Range(«A1:C3»).Value = a

End Sub

В данном случае переменная массива не обязательно должна быть универсального типа (As Variant), например, если бы в нее записывались только текстовые данные, ее можно было бы объявить как строковую (As String), и все бы работало.

Пример 2

Объявление обычной переменной универсального типа, присвоение ей значений из диапазона ячеек «A1:C3», записанных кодом первого примера, и вставка этих значений из полученного двумерного массива в диапазон «D10:F12»:

Sub Test2()

Dim a As Variant

a = Range(«A1:C3»)

Range(«D10:F12») = a

End Sub

Естественно, указанные диапазоны ячеек расположены на активном листе.

Пример 3

Допустим, на рабочем листе «Лист1» в ячейках «A1:A5» записано количество какого-то товара, а в ячейках «B1:B5» — его цена. Необходимо к этой информации добавить сумму каждого товара, умножив количество на цену, и перенести данные на «Лист2».

Sub Test3()

Dim a As Variant, i As Long

  a = Лист1.Range(«A1:C5»)

    For i = 1 To 5

      a(i, 3) = a(i, 1) _

      * a(i, 2)

    Next

  Лист2.Range(«A1:C5») = a

End Sub

Массив создан сразу с размерностью 5×3 с элементами под суммы. Даже если на первом листе в ячейках «C1:C5» есть какие-то значения, в массиве они будут перезаписаны результатами вычислений.

Копирование значений из массива в массив

Этот пример показывает, как в VBA Excel можно скопировать значения из одного массива в другой:

Sub Test4()

Dim arr1, arr2

    arr1 = Range(«G7:I11»)

    arr2 = arr1

    Range(«B2:D6») = arr2

End Sub


Using the shape of the Range

Another approach in creating a function for ArrayFromRange would be using the shape and size of the Range to determine how we should structure the array. This way we don’t have to load the data into an intermediate array to determine the dimension.

For instance, if the target range is only one cell, then we know we want to return an array with the single value in it Array(target.value).

Below is the complete function that should deal with all cases. Note, this uses the same technique of using the Application.Transpose method to reshape the array.

' Helper function that returns an array from a range with the
' correct dimensions. This fixes the issue of single values
' not returning as an array, and when a 2 dimension array is returned
' when it only has 1 dimension of data.
'
' @author Robert Todar <robert@roberttodar.com>
Public Function ArrayFromRange(ByVal target As Range) As Variant
    Select Case True
        ' Single cell
        Case target.Cells.Count = 1
            ArrayFromRange = Array(target.Value)
            
        ' Single Row
        Case target.Rows.Count = 1
            ArrayFromRange = Application.Transpose( _
                Application.Transpose(target.Value) _
            )
        
        ' Single Column
        Case target.Columns.Count = 1
            ArrayFromRange = Application.Transpose(target.Value)
            
        ' Multi dimension array
        Case Else
            ArrayFromRange = target.Value
    End Select
End Function

Testing the ArrayFromRange function

As a bonus, here are the tests that I ran to check that this function works.

' @requires {function} ArrayDimensionLength
' @requires {function} ArrayCount
Private Sub testArrayFromRange()
    ' Setup a new workbook/worksheet for
    ' adding testing data
    Dim testWorkbook As Workbook
    Set testWorkbook = Workbooks.Add
    Dim ws As Worksheet
    Set ws = testWorkbook.Worksheets(1)
    
    ' Add sample data for testing.
    ws.Range("A1:A2") = Application.Transpose(Array("A1", "A2"))
    ws.Range("B1:B2") = Application.Transpose(Array("B1", "B2"))
    
    ' This section will run all the tests.
    Dim x As Variant
    
    ' Single cell
    x = ArrayFromRange(ws.Range("A1"))
    Debug.Assert ArrayDimensionLength(x) = 1
    Debug.Assert ArrayCount(x) = 1
    
    ' Single Row
    x = ArrayFromRange(ws.Range("A1:B1"))
    Debug.Assert ArrayDimensionLength(x) = 1
    Debug.Assert ArrayCount(x) = 2
    
    ' Single Column
    x = ArrayFromRange(ws.Range("A1:A2"))
    Debug.Assert ArrayDimensionLength(x) = 1
    Debug.Assert ArrayCount(x) = 2
    
    ' Multi Column
    x = ArrayFromRange(ws.Range("A1:B2"))
    Debug.Assert ArrayDimensionLength(x) = 2
    Debug.Assert ArrayCount(x) = 4
    
    ' Cleanup testing environment
    testWorkbook.Close False
    
    ' Print result
    Debug.Print "testArrayFromRange: PASS"
End Sub

Helper functions for the tests

In my tests I used two helper functions: ArrayCount, and ArrayDimensionLength. These are listed below for reference.

' Returns the length of the dimension of an array
'
' @author Robert Todar <robert@roberttodar.com>
Public Function ArrayDimensionLength(sourceArray As Variant) As Integer
On Error GoTo catch
    Do
        Dim currentDimension As Long
        currentDimension = currentDimension + 1
        
        ' `test` is used to see when the
        ' Ubound throws an error. It is unused
        ' on purpose.
        Dim test As Long
        test = UBound(sourceArray, currentDimension)
    Loop
catch:
    ' Need to subtract one because the last
    ' one errored out.
    ArrayDimensionLength = currentDimension - 1
End Function
' Get count of elements in an array regardless of
' the option base. This Looks purely at the size
' of the array, not the contents within them such as
' empty elements.
'
' @author Robert Todar <robert@roberttodar.com>
' @requires {function} ArrayDimensionLength
Public Function ArrayCount(ByVal sourceArray As Variant) As Long
    Dim dimensions As Long
    dimensions = ArrayDimensionLength(sourceArray)
    
    Select Case dimensions
        Case 0
            ArrayCount = 0
        
        Case 1
            ArrayCount = (UBound(sourceArray, 1) - LBound(sourceArray, 1)) + 1
        
        Case Else
            ' Need to set arrayCount to 1 otherwise the
            ' loop will keep multiplying by zero for each
            ' iteration
            ArrayCount = 1
           
            Dim dimension As Long
            For dimension = 1 To dimensions
                ArrayCount = ArrayCount * _
                    ((UBound(sourceArray, dimension) - LBound(sourceArray, dimension)) + 1)
            Next
    End Select
End Function

Return to VBA Code Examples

In this Article

  • Assign Range to Array
    • Assign Value From a Single Column
    • Assign value from multiple columns

This tutorial will demonstrate how to populate an array with a range of cells.

Assign Range to Array

We can easily populate a Variant array with a range of cells.

Assign Value From a Single Column

This example will loop through Range(“A1:A10”), assigning the the cell values to an array:

Sub TestArrayValuesSingle()
'Declare the array as a variant array
   Dim arRng() As Variant

'Declare the integer to store the number of rows
   Dim iRw As Integer

'Assign range to a the array variable
   arRng = Range("A1:A10")

'loop through the rows - 1 to 10
   For iRw = 1 To UBound(arRng)

'show the result in the immediate window
      Debug.Print arRng(iRw , 1)
   Next iRw 
End Sub

The UBound is used to set the array upper bound (eg 10) so that the loop knows to loop 10 times.

The Debug.Print function will show you the value contained in the array in the immediate window.

Assign value from multiple columns

Sub TestArrayValuesMultiple()
'Declare the array as a variant array
   Dim arRng() As Variant 

'Declare the integer to store the number of rows
   Dim iRw As Integer

'Declare the integer to store the number of columns
   Dim iCol as Integer

'Assign range to a the array variable
   arRng = Range("A1:C10")

'loop through the rows - 1 to 10
   For iRw = 1 To UBound(arRng,1)

'now - while in row 1, loop through the 3 columns
      For iCol = 1 to UBound(arRng,2)

'show the result in the immediate window
         Debug.Print arRng(iRw, iCol)
      Next iCol
   Next iRw 
End Sub

In the code above, we have populated the array with the values in Range(“A1:C10”).

The UBound is once again used – but this time it is needed twice – once to loop through the rows, and then again to loop through the columns.

The Debug.Print function will show you the value contained in the array in the immediate window.

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!

Egor.V.A

0 / 0 / 2

Регистрация: 01.06.2012

Сообщений: 139

1

Записать в массив данные из диапазона ячеек

08.06.2015, 16:00. Показов 7809. Ответов 1

Метки нет (Все метки)


Студворк — интернет-сервис помощи студентам

Всем привет.
Не получается разобраться с массивом!
Необходимо залить в массив данные из диапазона ячеек (Важно! Без использования циклов, всё одним махом). После этого получить возможность выдавать результат конкретного номера компонента массива.
Что-то вроде этого:

Visual Basic
1
2
3
ReDim a(1 to 10) 'Пусть данные в ячейках будут не числовыми, строковые
a=Application.Transpose.Range(Cells(1, 1), Cells(1, 10)) 'одной комбинацией заливаем все данные диапазона в массив
MsgBox a(3) 'получить значение третьего номера в массиве

Только ошибку выдает.



0



Аксима

6076 / 1320 / 195

Регистрация: 12.12.2012

Сообщений: 1,023

08.06.2015, 18:44

2

Здравствуйте, Egor.V.A,

Visual Basic
1
Application.Transpose.Range(...)

Разве Transpose — это объект с методом Range?
По-моему, наоборот: Transpose — метод, Range — объект, передаваемый ему в качестве параметра.

Visual Basic
1
Range(Cells(1, 1), Cells(1, 10))

Вертикальный массив достаточно было бы транспонировать один раз, горизонтальный — необходимо транспонировать дважды.

Примерный вариант исправления:

Visual Basic
1
2
3
4
Dim a() As Variant
'ReDim a(1 to 10) Не нужно! Размер массива определяется автоматически.
a = Application.Transpose(Application.Transpose(Range(Cells(1, 1), Cells(1, 10))))
MsgBox a(3)

С уважением,

Аксима



1



Home / VBA / Arrays / VBA Range to an Array

Steps to Add a Range into an Array in VBA

  1. First, you need to declare a dynamic array using the variant data type.
    1-declare-a-dynamic-variable-1
  2. Next, you need to declare one more variable to store the count of the cells from the range and use that counter for the loop as well.
    2-declare-one-more-variable-2
  3. After that, assign the range where you have value to the array.
    3-assign-the-range-3
  4. From here, we need to create a loop to print all the values to the immediate window so that you can see that all the values are assigned to the array.
    4-create-a-loop-to-print-all-values-4
Sub myArrayRange()

Dim iAmount() As Variant
Dim iNum As Integer

iAmount = Range("A1:A11")

For iNum = 1 To UBound(iAmount)
    Debug.Print iAmount(iNum, 1)
Next iNum

End Sub

And when you run the above code it shows you all the values you have assigned from the range (“A1:A11) to the array iAmount and prints it to the immediate window.

In the same way, you can also use a multi-dimensional array.

Sub myArrayRange()

Dim iAmount() As Variant
Dim iNum1 As Integer

iAmount = Range("A1:B13")

For iNum1 = 1 To UBound(iAmount, 1)
        Debug.Print iAmount(iNum1, 1) & " " & iAmount(iNum1, 2)
Next iNum1

End Sub

Or you can also do this way as well.

Sub myArrayRange()

Dim iAmount() As Variant
Dim iNum1 As Integer
Dim iNum2 As Integer

iAmount = Range("A1:B13")

For iNum1 = 1 To UBound(iAmount, 1)
    For iNum2 = 1 To UBound(iAmount, 2)
        Debug.Print iAmount(iNum1, iNum2)
    Next iNum2
Next iNum1

End Sub

Понравилась статья? Поделить с друзьями:
  • Запись двоичных чисел в excel
  • Запись датафрейма в excel python
  • Запись данных на sd карту ардуино в excel
  • Запись данных в файл excel vba
  • Запись данных в таблицы word