Skip to content

Instantly share code, notes, and snippets.

@SeongilRyu
Last active January 13, 2019 04:08
Show Gist options
  • Save SeongilRyu/55706cf902f2b84e009a0c7fa7af695b to your computer and use it in GitHub Desktop.
Save SeongilRyu/55706cf902f2b84e009a0c7fa7af695b to your computer and use it in GitHub Desktop.
Arrays
Sub arrays_declare()
'방법 1 : Dim 사용
Dim arr1() 'Size 없이 선언
'방법 2 : Size를 지정하여 선언
Dim arr2(5) '사이트 5를 지정
'방법 3 : 'Array' 파라메터 사용
Dim arr3
arr3 = Array("사과","오렌지","포도")
End Sub
Sub array_assign()
''배열에 값 지정
Dim arr(5)
arr(0) = "1" '숫자 문자
arr(1) = "VBA" '문자
arr(2) = 100 '숫자
arr(3) = 3.14 '십진수
arr(4) = #2019-01-13# '날짜
arr(5) = #12.45 PM# '시간
msgbox("Array index 0의 값 : " & arr(0))
msgbox("Array index 1의 값 : " & arr(1))
msgbox("Array index 2의 값 : " & arr(2))
msgbox("Array index 3의 값 : " & arr(3))
msgbox("Array index 4의 값 : " & arr(4))
msgbox("Array index 5의 값 : " & arr(5))
End Sub
Sub array_multi_dimension()
''다차원 배열
Dim arr(2,3) as Variant ' (row 3, col 4)
arr(0,0) = "row 0, col 0"
arr(0,1) = "row 0, col 1"
arr(0,2) = "row 0, col 2"
arr(0,3) = "row 0, col 3"
arr(1,0) = "row 1, col 0"
arr(1,1) = "row 1, col 1"
arr(1,2) = "row 1, col 2"
arr(1,3) = "row 1, col 3"
arr(2,0) = "row 2, col 0"
arr(2,1) = "row 2, col 1"
arr(2,2) = "row 2, col 2"
arr(2,3) = "row 2, col 3"
msgbox("Array index 0,1의 값 : " & arr(0,1))
msgbox("Array index 2,2의 값 : " & arr(2,2))
End Sub
Sub arrays_redim()
''Size없이 배열 선언
Dim a() as variant
i = 0
''redim으로 크기 지정
redim a(3)
a(0) = "XYZ"
a(1) = 3.14
a(2) = 275
'배열값 확인
For i = 0 to UBound(a)
Msgbox a(i)
Next
End Sub
Sub array_split()
'Split function: 구분자로 분리될 수 있는 문자열을 분리하여 배열로 만든다.
' 구분자(delimiter) comma ','
Dim a as Variant
a = Split("Red,Blue,Yellow",",")
For i = 0 to UBound(a)
msgbox("Value" & i & " is :" & a(i))
Next
End Sub
Sub array_join()
'Join function: Split 함수의 반대 개념.
' 배열 항목들을 구분하여 문자열을 만든다.
a = array("Red","Blue","Yellow")
' Join using $
b = join(a,"$")
msgbox("The Join result after using delimiter is : " & b)
''Red$Blue$Yellow
End Sub
Sub array_filter()
'Filter function: 필터 조건에 맞는 항목만 선택된 배열을 만든다.
Dim a,b,c,d as Variant
a = array("Red","Blue","Yellow")
b = Filter(a,"B")
c = Filter(a,"e")
d = Filter(a,"Y")
For each x in b
msgbox("The Filter result 1: " & x)
Next
For each y in c
msgbox("The Filter result 2: " & y)
Next
For each z in d
msgbox("The Filter result 3: " & z)
Next
End Sub
Sub array_erase()
' IsArray function: 배열인지 아닌지 확인(True, False)
Dim a,b as Variant
a = array("Red","Blue","Yellow")
b = "12345"
If IsArray(a) = True then
Erase a ' 각 배열의 항목이 초기화 된다.
' Dynamic Array인 경우 사용된 메모리가 Free된다.
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment