2015-04-06 53 views
2

有使用smple班VBA的力學幾個非常好的ansewers的:When to use a Class in VBA?What are the benefits of using Classes in VBA?使用類在Excel中使用VBA多個範圍工作

正如有人relativley新OOP和類,它很難知道如何實施它們,甚至真的有可能。

例如,我必須處理多個工作表中的大範圍,並且需要獲取許多不同的數據子集。 「有x的代理商」和「有y的客戶」等。這裏有一個子我放在一起獲得在誰擁有超過2個客戶端代理的數量:

Sub agent_subset(output_sheet As String, _ 
            Input_sheet_name As String, _ 
            email_col As Integer, _ 
            vendor_count_col As Integer, _ 
            client_count_col As Integer, _ 
            modified_col As Integer, _ 
            num_of_clients As Integer) 
' get a list of all agents with 2 or more clients and put them into a sheet 

    Application.DisplayStatusBar = True 

    Dim sheet_rows As Long 
    sheet_rows = Worksheets(Input_sheet_name).Cells(rows.Count, 1).End(xlUp).Row 

    Dim email_range As Range ' range of agent emails 
    Dim client_count_range As Range ' range of client count 
    Dim vendor_count_range As Range ' range of vendor count 
    Dim modified_range As Range ' range of modified at 

    With Worksheets(Input_sheet_name) 
     Set email_range = .Range(.Cells(2, email_col), .Cells(sheet_rows, email_col)) 
     Set client_count_range = .Range(.Cells(2, client_count_col), .Cells(sheet_rows, client_count_col)) 
     Set vendor_count_range = .Range(.Cells(2, vendor_count_col), .Cells(sheet_rows, vendor_count_col)) 
     Set modified_range = .Range(.Cells(2, modified_col), .Cells(sheet_rows, modified_col)) 
    End With 

    Dim n As Long 
    Dim counter As Long 
    counter = 0 

    Dim modified_array() As String 

    For n = 2 To sheet_rows 
     If client_count_range(n, 1).Value > num_of_clients Then 
      counter = counter + 1 
      Worksheets(output_sheet).Cells(counter + 1, 1).Value = email_range(n, 1).Value 
      Worksheets(output_sheet).Cells(counter + 1, 2).Value = client_count_range(n, 1).Value 
      Worksheets(output_sheet).Cells(counter + 1, 3).Value = vendor_count_range(n, 1).Value 
       modified_array() = Split(modified_range(n, 1).Value, "T") 
      Worksheets(output_sheet).Cells(counter + 1, 4).Value = modified_array(0) 
     End If 

     Application.StatusBar = "Loop status: " & n & "of " & sheet_rows 

    Next n 

Worksheets(output_sheet).Cells(counter + 3, 1).Value = "Last run was " & Now() 

Application.StatusBar = False 

End Sub 

它的偉大工程,但現在我想根據其他標準獲得代理商和客戶提供一個更小的子集。所以,我會寫一個類似的函數,操縱類似的數據。我的直覺告訴我,使用課程會讓我的生活更輕鬆,但我不知道如何削減任務。

是否應該有一個Agent類,它包含有關代理的所有信息?和/或客戶類?或者,這些課程應該用於查看整個範圍還是工作表?

我不是在尋找特定的代碼,而是一種關於如何打破事情的方法論。

回答

1

我在多張

如果你想在計算機方面應對大範圍,你的代碼將是非常實用的。這些範圍代表真實的東西(代理商,客戶,發票,交易),所以用這些術語思考。

我會有一個代理類,它包含代理的所有屬性。我將擁有一個代理集合類,它可以容納我所有的代理類。然後,在我的Agents類中,我將有Filter方法返回Agents類的子集。

下面是一個例子:我有客戶。客戶可以是主動或不主動。當我向他們發送特定信息時,客戶也會使用我使用的模板。當我想以電子郵件誰使用表1模板活躍客戶,它看起來像這樣

Set clsCustomersToEmail = clsCustomers.FilterOnActive(True).FilterOnTemplate("Table1") 
For Each clsCustomer in clsCustomersToEmail 
    'Do stuff 
Next clsCustomer` 

在我CCustomers集合類,我有一對夫婦與幾個客戶比大CCustomers類返回CCustomers類的屬性(所謂clsCustomers)

Public Property Get FilterOnActive() As CCustomers 

    Dim clsReturn As CCustomers 
    Dim clsCustomer As CCustomer 

    Set clsReturn = New CCustomers 

    For Each clsCustomer In Me 
     If clsCustomer.Active Then 
      clsReturn.Add clsCustomer 
     End If 
    Next clsCustomer 

    Set FilterOnActive = clsReturn 

End Property 
Public Property Get FilterOnTemplate(ByVal sTemplate As String) As CCustomers 

    Dim clsReturn As CCustomers 
    Dim clsCustomer As CCustomer 

    Set clsReturn = New CCustomers 

    For Each clsCustomer In Me 
     If clsCustomer.Template.TemplateName = sTemplate Then 
      clsReturn.Add clsCustomer 
     End If 
    Next clsCustomer 

    Set FilterOnTemplate = clsReturn 

End Property 

當我想要做的東西像一堆客戶數據寫入到一個範圍,我作出這樣的返回數組的屬性,並寫入到陣列的範圍。

Set clsCustomersPastDue = clsCustomers.FilterOnActive(True).FilterOnPastDue(True) 
vaWrite = clsCsutomerPastDue.AgingReport 
rCell.Resize(UBound(vaWrite,1),UBound(vaWrite,2)).Value = vaWrite 

我傾向於想到我編碼的物理事物。他們不必是有形的東西,但如果你能把它帶回有形的東西,這可能會有所幫助。

交易是一件事情。但如果該交易是發票,現在確定該物品的屬性變得容易。首先,您可以查看紙質發票並查看屬性。

下一關是關係。每個Agent類都有客戶。所以你的Agent對象應該有一個返回一個CCustomers類的Customers屬性。或者,如果它在現實生活中的結構不嚴密,也許您的CCustomers類具有FilterOnAgent屬性,該屬性返回您正在查找的客戶的子集。

+0

這很棒!很有幫助!除此之外,我不確定一組類對象是否合理,即具有一個代理類,然後將其集合到一個集合中。非常感謝! – dwstein

相關問題