返回列表 上一主題 發帖

[發問] 請問 VBA 如何 add Object 到 ArrayList "ArrayList.Add(Object)"

回復 1# solitary0720
試試看用物件類別模組 取代 User-Defined Type

定義物件類別模組Test
  1. Public Name As String
  2. Public Num As Integer
  3. Public Bool As Boolean
複製代碼

TOP

本帖最後由 stillfish00 於 2013-12-19 13:01 編輯

回復 3# solitary0720
是指 VBA project 插入物件類別模組後,Class1 改名稱為 Test,填上
  1. Public Name As String
  2. Public Num As Integer
  3. Public Bool As Boolean
複製代碼
因為我不知道ArrayList在設定引用項目要勾選哪個
故改用CreateObject去late-binding,
一般模組中:
  1.    
  2. Public ArrList As Object  'New ArrayList
  3. Function Body() As String
  4.     Dim Test_1 As Test

  5.     Set ArrList = CreateObject("system.collections.arraylist")
  6.     Set Test_1 = New Test

  7.     Test_1.Name = "sophia"   
  8.     ArrList.Add Test_1

  9. End Function
複製代碼
話說回來,一般VBA中比較常見的不是 arraylist ,而是用 array 或 dictionary。

TOP

回復 6# solitary0720
If ArrList(3) is Nothing Then

TOP

        靜思自在 : 世上有兩件事不能等:一、孝順 二、行善。
返回列表 上一主題