[Original] VBA study notes (304) VBA basics array array (2): Array is more suitable for the whole process is more suitable for the operation of each element of the dictionary

 

An array like a whole, like a dictionary individually set for processing.

  • Design arrays and dictionaries of people, very different purpose

  • Understand the array and positioning of both the dictionary there are many differences

 

1.1 features an array of

---- 1.1.1 convenient array operation method is best to use the array: circular

  • A whole array assignment arr1 = range ()
  • (Dynamic) arrays overall change in assignment
  • Get the upper and lower limit of the array, lbound () and ubound ()

 

1.1.2 array inconvenient operation

  • Count the number of array? It is very difficult
  • Elements of the array how to add? It is very difficult
  • Elements of the array how to delete? It is very difficult, it seems can not be deleted, only blank

 

1.1.3 Main operating method of the array

  • Substantially for each cycle, or rely for i = 1 to 2 
  • Can array (i) the way the operation is also an element, but not convenient dictionary

 

1.1.4 array of designer intent ------ - to use an array as a whole

  • The overall feel is more suitable for processing, rather than dealing with an element inside
  • In VBA using an array of purposes, usually in order to facilitate efficient.
  • How to achieve convenient and efficient, it is to deal with the array as a whole.

 

1.1.5 Array can easily generate an array of other methods, filter () Screening - to achieve the effect of deleting an element

Sub test1001()
arr1 = Array(1, 2, 3, 4, 5, 5, 5)

arr2 = Filter(arr1, 5, True)
show_arr1 (arr2)


arr3 = Filter(arr1, 5, False)
show_arr1 (arr3)

End Sub


Function show_arr1(x)  '写的这个只能针对一维数组

For Each I In x
   Debug.Print I;
Next
Debug.Print
End Function

 

1.2 dictionary features

1.2.1 The dictionaries are convenient methods and properties (built a lot of useful methods and properties)

  • Dictionary key is not duplicated
  • You can find a dictionary dict (key), if no item is added
  • Dictionary very easily, deleted, added element dictionary
  • Dict.add
  • Dict.remove()
  • Dict.removeall()

 

1.2.2 Dictionary property

  • dict () assignment with
  • dict.item () is equivalent to dict () 
  • dict.key () is special, used to change the key value
  • dict.count
  • dict.comparemode

 

1.2.3 dictionary approach

  • dict.add key item add a couple of key-item
  • dict.remove(key)    
  • dict.removeall
  • dict.exists(key)
  • dict.keys () to generate an array of 1
  • dict.items () to generate an array of 2

 

1.3 Comparison of

  • An array of open like a whole is not suitable for processing, and adapted to process a single set of dictionaries like element
  • More like a whole array
  • More like a collection of dictionaries
  • If you are in the process, you need to delete a value, or a collection of dictionaries is commonly used, instead of an array.
  • The purpose of the designer obviously, is when the overall use it as much as possible so

 

2 If you have to delete an element of an array

  • Method 1: blank, but still retains this position
  • Method 2: Delete the process cycle and then automatically generate a new array, the array does not change because the original

Delete some elements in the array and rebuild the array does not contain these elements,

If you must do so, in general loop through is the only correct approach.

 

Sub test_arr1()

arr1 = Range("a1:a10")

For I = LBound(arr1) To UBound(arr1)
   For J = LBound(arr1, 2) To UBound(arr1, 2)
       Debug.Print "arr1(" & I & "," & J & ")="; arr1(I, J)
   Next
Next
Debug.Print


arr1(1, 1) = ""
Debug.Print "置空单元格后"
For I = LBound(arr1) To UBound(arr1)
   For J = LBound(arr1, 2) To UBound(arr1, 2)
       Debug.Print "arr1(" & I & "," & J & ")="; arr1(I, J)
   Next
Next
Debug.Print



Debug.Print "删除某个元素"
Dim arr2()
m = 1
For I = LBound(arr1) To UBound(arr1)
   For J = LBound(arr1, 2) To UBound(arr1, 2)
       If Not arr1(I, J) = 4 Then
          ReDim arr2(m)
          arr2(m) = arr1(I, J)
          Debug.Print "arr2(" & m & ")="; arr2(m)
          m = m + 1
       Else
       
       End If
   Next
Next
Debug.Print


End Sub

 

3 To perform deduplication processing array (using dictionary)

 

Sub test_arr2()

arr1 = Range("a1:a10")


Dim dict1 As Object
Set dict1 = CreateObject("scripting.dictionary")

For Each I In arr1
   dict1(I) = ""
Next


Debug.Print "被字典净化去重后的数组"
For I = LBound(arr1) To UBound(arr1)
   For J = LBound(arr1, 2) To UBound(arr1, 2)
       Debug.Print "arr1(" & I & "," & J & ")="; arr1(I, J)
   Next
Next
Debug.Print

Debug.Print "被字典净化去重后的数组变化的新数组dict1.keys()"
For Each I In dict1.keys()
    Debug.Print I
Next



End Sub

 

Published 370 original articles · won praise 45 · views 90000 +

Guess you like

Origin blog.csdn.net/xuemanqianshan/article/details/103893773