VBA - Função Apagar

A função Erase é usada para redefinir os valores de arrays de tamanho fixo e liberar a memória dos arrays dinâmicos. Ele se comporta dependendo do tipo de matrizes.

Sintaxe

Erase ArrayName
  • Matriz numérica fixa, cada elemento em uma matriz é redefinido como Zero.
  • Matriz de string fixa, cada elemento em uma matriz é redefinido para comprimento zero "".
  • Matriz de objetos, cada elemento em uma matriz é redefinido para um valor especial Nothing.

Exemplo

Adicione um botão e adicione a seguinte função.

Private Sub Constant_demo_Click()
   Dim NumArray(3)
   NumArray(0) = "VBScript"
   NumArray(1) = 1.05
   NumArray(2) = 25
   NumArray(3) = #23/04/2013#
  
   Dim DynamicArray()
   ReDim DynamicArray(9)   ' Allocate storage space.
  
   Erase NumArray          ' Each element is reinitialized.
   Erase DynamicArray      ' Free memory used by array.
  
   ' All values would be erased.
   msgbox("The value at Zeroth index of NumArray is " & NumArray(0))
   msgbox("The value at First index of NumArray is " & NumArray(1))
   msgbox("The value at Second index of NumArray is " & NumArray(2))
   msgbox("The value at Third index of NumArray is " & NumArray(3))
End Sub

Quando você executa a função acima, ela produz a seguinte saída.

The value at Zeroth index of NumArray is 
The value at First index of NumArray is 
The value at Second index of NumArray is 
The value at Third index of NumArray is