簡體   English   中英

如何從工作表數據填充用戶定義的類型數組

[英]How to populate user-defined type array from worksheet data

我有一個用戶定義類型的數組,並希望從工作表中獲取數據到此數組。 我有一個解決方案,但它似乎不優雅。 有沒有更好或更簡單的方法來做到這一點?

Type Donation
    NBID As Integer
    Amount As Single
    DonationDate As Date
    TrackingCode As String
End Type

Public dons() as Donation

Sub init()
    Dim i As Integer
    Dim tmpDons() As Variant
    Dim donRows as Integer

    tmpDons = Sheets("Appeal Dons").UsedRange.Value2
    donRows = UBound(tmpDons)
    ReDim dons(donRows - 2)

    For i = 2 To donRows
        dons(i - 2).NBID = tmpDons(i, 1)
        dons(i - 2).Amount = tmpDons(i, 2)
        dons(i - 2).TrackingCode = tmpDons(i, 3)
        dons(i - 2).DonationDate = tmpDons(i, 4)
    Next
End Sub

嘗試:

Option Explicit

Sub test()

    Dim arr As Variant
    Dim i As Long

    With ThisWorkbook.Worksheets("Sheet1")
        'Import to array the used range
        arr = .UsedRange

        'Loop from L to U arr bound
        For i = LBound(arr) To UBound(arr)
            'Code
        Next i

    End With

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM