VBA ReDim | VBA ReDim Preserve를 사용하여 동적 배열 처리

Excel VBA ReDim 문

VBA Redim 문은 dim 문과 비슷하지만 차이점은 더 많은 저장 공간을 저장 또는 할당하거나 변수 또는 배열에 포함 된 저장 공간을 줄이는 데 사용된다는 것입니다. 이제 문과 함께 사용되는 두 가지 중요한 측면이 Preserve입니다. 이 문에서 preserve를 사용하면 크기가 다른 새 배열을 만들고이 문에서 preserve를 사용하지 않으면 현재 변수의 배열 크기 만 변경합니다.

배열은 VBA 코딩의 중요한 부분입니다. 배열을 사용하면 우리가 정의한 동일한 변수에 둘 이상의 값을 저장할 수 있습니다. "Dim"이라는 단어를 사용하여 변수를 선언하는 방법과 마찬가지로 "Dim"을 사용하여 배열 이름을 선언해야합니다.

배열 이름을 선언하려면 먼저 정의 할 배열의 종류를 식별해야합니다. 배열에는 5 가지 유형이 있습니다.

  1. 정적 배열
  2. 동적 배열
  3. 1 차원 배열
  4. 2 차원 배열
  5. 다차원 배열

엑셀의 정적 배열에서 우리는 변수를 선언하면서 배열의 하한값과 상한값을 미리 결정합니다. 예를 들어, 아래 예를보십시오.

암호:

 Sub ReDim_Example1 () Dim MyArray (1 ~ 5) As String End Sub 

여기서 MyArray 는 1에서 5까지의 값을 저장할 수있는 배열의 이름입니다. MyArray는 아래와 같이 5 개의 다른 결과를 저장할 수 있습니다.

암호:

 Sub ReDim_Example1 () Dim MyArray (1 ~ 5) As String MyArray (1) = "Hi"MyArray (2) = "Good"MyArray (3) = "Morning"MyArray (4) = "Have a"MyArray (5) = "Nice Day"End Sub 

ReDim 문이있는 동적 배열

그러나 Dynamic array에서는 그렇지 않습니다. 낮은 값과 높은 값을 미리 결정하지 않고 배열 이름을 정의하고 데이터 유형을 할당하기 만하면됩니다.

 Sub ReDim_Example1 () Dim MyArray () As String End Sub 

배열 이름을 동적으로 만들려면 먼저 "Dim"이라는 단어로 선언해야하지만 배열의 크기를 미리 결정하지 마십시오. 괄호 () 안에 빈 값이있는 배열의 이름을 지정합니다. 배열에 크기가 포함되지 않은 경우 동적 배열로 처리됩니다.

Dim MyArray () As String

괄호 안에 배열의 크기를 언급하는 순간 정적 배열이됩니다. Dim MyArray (1 ~ 5) As String

동적 배열에서 우리는 항상 코드의 다음 줄에 "ReDim"이라는 단어를 사용하여 배열 크기를 조정합니다.

ReDim MyArray (1 ~ 6) As String

이전 단계에서 "Dim"문을 사용하여 배열 이름에 저장된 모든 값은 null을 의미하며 "ReDim"을 사용하여 선언 한 크기가 배열의 새 크기가됩니다.

VBA Redim 문 사용 예제

이 VBA ReDim Excel 템플릿은 여기에서 다운로드 할 수 있습니다 – VBA ReDim Excel 템플릿

예 1

"ReDim"문을 실제로 사용하는 예를 살펴보십시오. 아래 단계에 따라 "ReDim"을 적용하십시오.

1 단계 : 먼저 매크로 이름을 만듭니다.

2 단계 : 배열 이름을 문자열로 선언합니다.

암호:

 Sub ReDim_Example1 () Dim MyArray () As String End Sub 

3 단계 : 이제 "Redim"이라는 단어를 사용하고 배열의 크기를 할당합니다.

암호:

 Sub ReDim_Example1 () Dim MyArray () As String ReDim MyArray (1 To 3) End Sub 

4 단계 : 이제 배열 이름 "MyArray"에 최대 3 개의 값을 저장할 수 있습니다. 다음과 같이이 3 개의 배열에 값을 할당합니다.

암호:

 Sub ReDim_Example1 () Dim MyArray () As String ReDim MyArray (1 To 3) MyArray (1) = "Welcome"MyArray (2) = "to"MyArray (3) = "VBA"End Sub 

따라서 첫 번째 배열은 "Welcome"이라는 단어와 같고 두 번째 배열은 "to"라는 단어와 같고 세 번째 배열은 "VBA"라는 단어와 같습니다.

5 단계 : 이제 이러한 배열 값을 셀에 저장합니다.

암호:

 Sub ReDim_Example1 () Dim MyArray () As String ReDim MyArray (1 To 3) MyArray (1) = "Welcome"MyArray (2) = "to"MyArray (3) = "VBA"Range ( "A1"). Value = MyArray (1) Range ( "B1"). Value = MyArray (2) Range ( "C1"). Value = MyArray (3) End Sub 

Step 6: If you run this code we should have these values in A1, B1, and C1 cell respectively.

Example #2 – Resize the Array Size While Remembering the Old Values.

Once the array name assigned values we can also resize at any point of time in the procedure by using the word “ReDim Preserve”.

Assume you have already declared an array name and assigned values to those array name like the below one.

Now you would like to increase the array length by 2 i.e. 5. In this case, we can use the word VBA “ReDim Preserve” to resize the array length to remember the old values as well.

Code:

 Sub ReDim_Example2() Dim MyArray() As String ReDim MyArray(3) MyArray(1) = "Welcome" MyArray(2) = "to" MyArray(3) = "VBA" ReDim Preserve MyArray(4) MyArray(4) = "Character 1" Range("A1").Value = MyArray(1) Range("B1").Value = MyArray(2) Range("C1").Value = MyArray(3) Range("D1").Value = MyArray(4) End Sub 

Now we can assign two more values to the array.

Code:

 Sub ReDim_Example2() Dim MyArray() As String ReDim MyArray(3) MyArray(1) = "Welcome" MyArray(2) = "to" MyArray(3) = "VBA" ReDim Preserve MyArray(4) MyArray(4) = "Character 1" Range("A1").Value = MyArray(1) Range("B1").Value = MyArray(2) Range("C1").Value = MyArray(3) Range("D1").Value = MyArray(4) End Sub 

Now store these values in cells.

Code:

 Sub ReDim_Example2() Dim MyArray() As String ReDim MyArray(3) MyArray(1) = "Welcome" MyArray(2) = "to" MyArray(3) = "VBA" ReDim Preserve MyArray(4) MyArray(4) = "Character 1" Range("A1").Value = MyArray(1) Range("B1").Value = MyArray(2) Range("C1").Value = MyArray(3) Range("D1").Value = MyArray(4) End Sub 

Now run the macro and see what happens

So we got the new word in the D1 cell.

The reason why we need to use the word “preserve” because array should remember the old array values in the procedure.

The moment you ignore the word “preserve” it will not remember old values.

Things to Remember Here

  • ReDim can only hold the last value of the array, not the many values. For example, we cannot use this code “ReDim Preserve MyArray(4 to 5)”, this will throw the error.
  • We cannot ReDim static arrays. The moment you assign the size of the array inside the parenthesis it becomes a static array.
  • Using ReDim we cannot change the data type. The array can hold whatever the data type we have assigned while declaring the array.