2012-09-13 41 views
31

我正嘗試在VBA for Excel中創建自定義數據類型。我們稱這種數據類型爲「卡車」。每輛卡車具有以下屬性:在VBA中使用自定義數據類型

NumberOfAxles (this is an integer) 
AxleWeights (this is an array of doubles) 
AxleSpacings (this is an array of doubles) 

我可以創建數據類型爲「卡車」的許多實例(卡車(1),貨車(2)...等),和讀/寫我列出的屬性上面那個例子?

實施例:

Truck(1).NumberOfAxles = 2 
Truck(1).AxleWeights(1) = 15.0 
Truck(1).AxleWeights(2) = 30.0 
Truck(1).AxleSpacings(1) = 8.0 

Truck(2).NumberOfAxles = 3 
Truck(2).AxleWeights(1) = 8.0 
Truck(2).AxleWeights(2) = 10.0 
Truck(2).AxleWeights(3) = 12.0 
Truck(2).AxleSpacings(1) = 20.0 
Truck(2).AxleSpacings(2) = 4.0 

等。上面的語法很可能是錯誤的,我只是想演示我需要的結構。

所有我試圖將數據寫入到數據結構,並在必要時如

Truck(i).NumberOfAxles 
Truck(i).AxleWeights(j) 
Truck(i).AxleSpacings(j) 

非常感謝你打電話吧!當然

回答

51

,您可以:

Option Explicit 

'***** User defined type 
Public Type MyType 
    MyInt As Integer 
    MyString As String 
    MyDoubleArr(2) As Double 
End Type 

'***** Testing MyType as single variable 
Public Sub MyFirstSub() 
    Dim MyVar As MyType 

    MyVar.MyInt = 2 
    MyVar.MyString = "cool" 
    MyVar.MyDoubleArr(0) = 1 
    MyVar.MyDoubleArr(1) = 2 
    MyVar.MyDoubleArr(2) = 3 

    Debug.Print "MyVar: " & MyVar.MyInt & " " & MyVar.MyString & " " & MyVar.MyDoubleArr(0) & " " & MyVar.MyDoubleArr(1) & " " & MyVar.MyDoubleArr(2) 
End Sub 

'***** Testing MyType as an array 
Public Sub MySecondSub() 
    Dim MyArr(2) As MyType 
    Dim i As Integer 

    MyArr(0).MyInt = 31 
    MyArr(0).MyString = "VBA" 
    MyArr(0).MyDoubleArr(0) = 1 
    MyArr(0).MyDoubleArr(1) = 2 
    MyArr(0).MyDoubleArr(2) = 3 
    MyArr(1).MyInt = 32 
    MyArr(1).MyString = "is" 
    MyArr(1).MyDoubleArr(0) = 11 
    MyArr(1).MyDoubleArr(1) = 22 
    MyArr(1).MyDoubleArr(2) = 33 
    MyArr(2).MyInt = 33 
    MyArr(2).MyString = "cool" 
    MyArr(2).MyDoubleArr(0) = 111 
    MyArr(2).MyDoubleArr(1) = 222 
    MyArr(2).MyDoubleArr(2) = 333 

    For i = LBound(MyArr) To UBound(MyArr) 
     Debug.Print "MyArr: " & MyArr(i).MyString & " " & MyArr(i).MyInt & " " & MyArr(i).MyDoubleArr(0) & " " & MyArr(i).MyDoubleArr(1) & " " & MyArr(i).MyDoubleArr(2) 
    Next 
End Sub 
+0

很好的解釋!非常感謝! – marillion

+0

不客氣! @ooo關於類的答案也適用於你。 –

+1

我檢查了ooo的答案,並且看到了使用類而不是類型的好處。我同意使用類將會使代碼更具前瞻性,但您的答覆能夠快速解決我的具體問題(數據結構非常簡單且有限)。 – marillion

27

看起來要定義卡車與性能NumberOfAxles,AxleWeights & AxleSpacings一個Class

這可以在類模塊定義(這裏命名clsTrucks

Option Explicit 

Private tID As String 
Private tNumberOfAxles As Double 
Private tAxleSpacings As Double 


Public Property Get truckID() As String 
    truckID = tID 
End Property 

Public Property Let truckID(value As String) 
    tID = value 
End Property 

Public Property Get truckNumberOfAxles() As Double 
    truckNumberOfAxles = tNumberOfAxles 
End Property 

Public Property Let truckNumberOfAxles(value As Double) 
    tNumberOfAxles = value 
End Property 

Public Property Get truckAxleSpacings() As Double 
    truckAxleSpacings = tAxleSpacings 
End Property 

Public Property Let truckAxleSpacings(value As Double) 
    tAxleSpacings = value 
End Property 

然後在MODULE以下定義了一個新的卡車和它的屬性,並將其添加到集合卡車,然後檢索收集。

Option Explicit 

Public TruckCollection As New Collection 

Sub DefineNewTruck() 
Dim tempTruck As clsTrucks 
Dim i As Long 

    'Add 5 trucks 
    For i = 1 To 5 
     Set tempTruck = New clsTrucks 
     'Random data 
     tempTruck.truckID = "Truck" & i 
     tempTruck.truckAxleSpacings = 13.5 + i 
     tempTruck.truckNumberOfAxles = 20.5 + i 

     'tempTruck.truckID is the collection key 
     TruckCollection.Add tempTruck, tempTruck.truckID 
    Next i 

    'retrieve 5 trucks 
    For i = 1 To 5 
     'retrieve by collection index 
     Debug.Print TruckCollection(i).truckAxleSpacings 
     'retrieve by key 
     Debug.Print TruckCollection("Truck" & i).truckAxleSpacings 

    Next i 

End Sub 

有這樣做的幾種方法,因此實際上取決於你打算如何使用數據作爲對一類/集合是否是最佳的設置或數組/詞典等

+6

感謝您解釋使用類的方法。我今天實施這個只是爲了熟悉,它的作用就像一個魅力。就我的目的而言,「自定義類型」方法有效,因爲未來這個項目不會有任何擴展。但是,我會嘗試在即將開展的項目中更頻繁地使用課程。謝謝! – marillion

相關問題