初始化VBA类 - 寻找更优雅的解决方案

时间:2014-06-30 09:47:53

标签: excel vba excel-vba

我目前正在开展一个项目,我想初始化两个不同的类。我使用集合类来存储工作表数据。

首先,我想要遍历课程'性能;遗憾的是,在VBA中似乎没有可能(没有制作一些黑客攻击解决方案)。

以下两个功能几乎相同,只有一个属性不同。我可以以某种方式创建一个在VBA中更可重用的函数吗?

Private Sub PasteCMSData()

    Dim PipelineCMSData As Collection
    Dim WonCMSData As Collection
    Dim I As Integer: I = 0

    Set PipelineCMSData = CollectPipeline()
    Set WonSheetName = CollectWon()

End Sub

私人功能CollectPipeline()作为集合

Private Function CollectPipeline() As Collection

    Const StartPos As Integer = 2

    Dim I As Integer: I = 0
    Dim PL As cPipeline
    Dim WorkbookData As Worksheet

    Set CollectPipeline = New Collection

    Set WorkbookData = Workbooks(WorkbookName).Worksheets(PLSheetName)

    For I = StartPos To WorkbookData.UsedRange.Rows.Count
        Set PL = New cPipeline
        With PL
            .ProjectType = WorkbookData.Cells(I, PLProjectType)
            .Segment = WorkbookData.Cells(I, PLSegment)
            .Customer = WorkbookData.Cells(I, PLCustomer)
            .Project = WorkbookData.Cells(I, PLProject)
            .Note = WorkbookData.Cells(I, PLNote)
            .CRM = WorkbookData.Cells(I, PLCRM)
            .Probability = WorkbookData.Cells(I, PLProbability)
            .Owner = WorkbookData.Cells(I, PLOwner)
            .SalesPhase = WorkbookData.Cells(I, PLSalesPhase)
            .NREPotential = WorkbookData.Cells(I, PLNREPotential)
            .RoyaltyPotential = WorkbookData.Cells(I, PLRoyaltyPotential)
            .Defcon = WorkbookData.Cells(I, PLDefcon)
            .ProjectStart = WorkbookData.Cells(I, PLProjectStart)
            .ProjectDuration = WorkbookData.Cells(I, PLProjectDuration)
        End With
        CollectPipeline.Add PL
    Next I

End Function

私人功能CollectWon()作为集合

Private Function CollectWon() As Collection

    Const StartPos As Integer = 2

    Dim I As Integer: I = 0
    Dim WO As cWon
    Dim WorkbookData As Worksheet

    Set CollectWon = New Collection

    Set WorkbookData = Workbooks(WorkbookName).Worksheets(WonSheetName)

    For I = StartPos To WorkbookData.UsedRange.Rows.Count
        Set WO = New cWon
        With WO
            .ActualCloseDate = WorkbookData.Cells(I, WOActualCloseDate)
            .ProjectType = WorkbookData.Cells(I, WOProjectType)
            .Segment = WorkbookData.Cells(I, WOSegment)
            .Customer = WorkbookData.Cells(I, WOCustomer)
            .Project = WorkbookData.Cells(I, WOProject)
            .Note = WorkbookData.Cells(I, WONote)
            .CRM = WorkbookData.Cells(I, WOCRM)
            .Probability = WorkbookData.Cells(I, WOProbability)
            .Owner = WorkbookData.Cells(I, WOOwner)
            .SalesPhase = WorkbookData.Cells(I, WOSalesPhase)
            .NREPotential = WorkbookData.Cells(I, WONREPotential)
            .RoyaltyPotential = WorkbookData.Cells(I, WORoyaltyPotential)
            .Defcon = WorkbookData.Cells(I, WODefcon)
            .ProjectStart = WorkbookData.Cells(I, WOProjectStart)
            .ProjectDuration = WorkbookData.Cells(I, WOProjectDuration)
        End With
        CollectWon.Add WO
    Next I

End Function

2 个答案:

答案 0 :(得分:1)

你可以;

  • 创建一个CommonAttributes类,用于存储两个类(.ProjectType.ProjectDuration)共享的数据。 cWon和& cPipeline持有此类的内部实例。

  • 将初始化逻辑移动到类中:CollectionWon.Add PL.LoadFromRange(I)

  • 在类中创建索引值枚举;

    enum WOStuff WOActualCloseDate = 1 WONREPotential = 9 ... end enum

LoadFromRange解析&存储任何特定于类的数据(.ActualCloseDate),创建CommonAttributes的实例并调用加载公共数据的方法:

myCommonAttributes.LoadFromRange(I, [paramArray of enum indexes])

答案 1 :(得分:0)

这永远不会太优雅,但可以在初始化变量列表上进行一项改进。

在保存它们的工作表上,您可以将它们放在两列{UniqueKey} {Value}对中,并将该范围转换为表格Ctrl-NT

可以为表提供一些列名,例如Key和Value,以及唯一的名称。然后在VBA中,您可以通过以编程方式查找Key并查找其对应的值来访问这些值。这样就可以对表进行重新排序,并且可以“错过”一些键,允许VBA代码以预先编程的默认值进行泵送。

可以在VBA中将该表作为ListObject进行访问。


在键值对的内部存储上。如果更适合您的实施,您可能需要考虑将其存储在Dictionary中。这样,您可以按键查找对象,而不是像Collection一样按顺序查找。

点击此处查看词典示例:

Does VBA have Dictionary Structure?