2011-06-14 56 views
0

我一直在創建幾個函數,所有這些函數都有很多相同的代碼。有問題的代碼是從Dim Order_Type As Range降到Set PMethod10 = Sheets("KRONOS").Range("$BK:$BK")VBA存儲全球使用代碼

我怎樣才能把它放在一個全局函數或類似的東西,以便只調用該函數,而不是每次都使用整個代碼?

Public Function BANKING1(rev_date As Date) As Variant 

    Dim Order_Type As Range 
    Dim Final_Price As Range 
    Dim PaidAlt As Range 
    Dim Excl_Rev As Range 
    Dim PAmount1 As Range 
    Dim PMethod1 As Range 
    Dim PAmount2 As Range 
    Dim PayDate2 As Range 
    Dim PMethod2 As Range 
    Dim Vstatus As Range 
    Dim Team As Range 

    Application.Volatile (True) 

    Set Order_Type = Sheets("KRONOS").Range("$D:$D") 
    Set Final_Price = Sheets("KRONOS").Range("$H:$H") 
    Set PaidAlt = Sheets("KRONOS").Range("$I:$I") 
    Set Excl_Rev = Sheets("KRONOS").Range("$K:$K") 
    Set Vstatus = Sheets("KRONOS").Range("$DL:$DL") 
    Set Team = Sheets("KRONOS").Range("$DO:$DO") 

    Set PAmount1 = Sheets("KRONOS").Range("$O:$O") 
    Set First_PD = Sheets("KRONOS").Range("$Q:$Q") 
    Set PMethod1 = Sheets("KRONOS").Range("$R:$R") 

    Set PAmount2 = Sheets("KRONOS").Range("$T:$T") 
    Set PayDate2 = Sheets("KRONOS").Range("$V:$V") 
    Set PMethod2 = Sheets("KRONOS").Range("$W:$W") 

    Set PAmount3 = Sheets("KRONOS").Range("$Y:$Y") 
    Set PayDate3 = Sheets("KRONOS").Range("$AA:$AA") 
    Set PMethod3 = Sheets("KRONOS").Range("$AB:$AB") 

    Set PAmount4 = Sheets("KRONOS").Range("$AD:$AD") 
    Set PayDate4 = Sheets("KRONOS").Range("$AF:$AF") 
    Set PMethod4 = Sheets("KRONOS").Range("$AG:$AG") 

    Set PAmount5 = Sheets("KRONOS").Range("$AI:$AI") 
    Set PayDate5 = Sheets("KRONOS").Range("$AK:$AK") 
    Set PMethod5 = Sheets("KRONOS").Range("$AL:$AL") 

    Set PAmount6 = Sheets("KRONOS").Range("$AN:$AN") 
    Set PayDate6 = Sheets("KRONOS").Range("$AP:$AP") 
    Set PMethod6 = Sheets("KRONOS").Range("$AQ:$AQ") 

    Set PAmount7 = Sheets("KRONOS").Range("$AS:$AS") 
    Set PayDate7 = Sheets("KRONOS").Range("$AU:$AU") 
    Set PMethod7 = Sheets("KRONOS").Range("$AV:$AV") 

    Set PAmount8 = Sheets("KRONOS").Range("$AX:$AX") 
    Set PayDate8 = Sheets("KRONOS").Range("$AZ:$AZ") 
    Set PMethod8 = Sheets("KRONOS").Range("$BA:$BA") 

    Set PAmount9 = Sheets("KRONOS").Range("$BC:$BC") 
    Set PayDate9 = Sheets("KRONOS").Range("$BE:$BE") 
    Set PMethod9 = Sheets("KRONOS").Range("$BF:$BF") 

    Set PAmount10 = Sheets("KRONOS").Range("$BH:$BH") 
    Set PayDate10 = Sheets("KRONOS").Range("$BJ:$BJ") 
    Set PMethod10 = Sheets("KRONOS").Range("$BK:$BK") 


      BANKING1 = Application.WorksheetFunction.SumIfs(_ 
      PAmount1 _ 
      , Team, "<>9" _ 
      , Vstatus, "<>rejected", Vstatus, "<>unverified" _ 
      , Excl_Rev, "<>1" _ 
      , PMethod1, "<>Credit" _ 
      , PMethod1, "<>Amendment" _ 
      , PMethod1, "<>Pre-paid" _ 
      , PMethod1, "<>Write Off" _ 
      , First_PD, rev_date) 

End Function 
+0

爲什麼當你不調暗它或在代碼中的任何地方使用它時,你有PAmount3?您是否擁有BANKING1功能,BANKING2功能,BANKING3功能等? – 2011-06-14 20:01:33

回答

3

您沒有返回任何值,因此您不需要創建函數就可以使用子例程。但是,因爲你所做的全部都是設置範圍變量,所以你可能只需要把它們全部命名爲範圍,並刪除所有的代碼。然後你會引用它們像這樣:

Range("Final_Price").Value = 30 

這裏有一個video on Named Rangeshere's another,這裏的a tutorial

+0

但是......他正在返回一些值。變量的血腥牆被用作'SumIfs'的範圍參數,對此''BANKING1'函數是一個包裝器。它被設計爲從工作表中調用,考慮Application.Volatile。 – GSerg 2011-06-14 16:51:28

+0

@Gserg,不,如果你看看他的'代碼問題陳述',那只是他想在函數中使用的Dims和Sets。 – 2011-06-14 20:28:54

2

你不能讓你的變量聲明別的,但你可以委託一個對象來爲你保存它們。

創建一個類,並把這個代碼到其中:

Option Explicit 

Public Order_Type As Range 
Public Final_Price As Range 
Public PaidAlt As Range 
Public Excl_Rev As Range 
Public PAmount1 As Range 
Public PMethod1 As Range 
Public PAmount2 As Range 
Public PayDate2 As Range 
Public PMethod2 As Range 
Public Vstatus As Range 
Public Team As Range 


Private Sub Class_Initialize() 
    Set Order_Type = Sheets("KRONOS").Range("$D:$D") 
    Set Final_Price = Sheets("KRONOS").Range("$H:$H") 
    Set PaidAlt = Sheets("KRONOS").Range("$I:$I") 
    Set Excl_Rev = Sheets("KRONOS").Range("$K:$K") 
    Set Vstatus = Sheets("KRONOS").Range("$DL:$DL") 
    Set Team = Sheets("KRONOS").Range("$DO:$DO") 

    Set PAmount1 = Sheets("KRONOS").Range("$O:$O") 
    Set First_PD = Sheets("KRONOS").Range("$Q:$Q") 
    Set PMethod1 = Sheets("KRONOS").Range("$R:$R") 

    Set PAmount2 = Sheets("KRONOS").Range("$T:$T") 
    Set PayDate2 = Sheets("KRONOS").Range("$V:$V") 
    Set PMethod2 = Sheets("KRONOS").Range("$W:$W") 

    Set PAmount3 = Sheets("KRONOS").Range("$Y:$Y") 
    Set PayDate3 = Sheets("KRONOS").Range("$AA:$AA") 
    Set PMethod3 = Sheets("KRONOS").Range("$AB:$AB") 

    Set PAmount4 = Sheets("KRONOS").Range("$AD:$AD") 
    Set PayDate4 = Sheets("KRONOS").Range("$AF:$AF") 
    Set PMethod4 = Sheets("KRONOS").Range("$AG:$AG") 

    Set PAmount5 = Sheets("KRONOS").Range("$AI:$AI") 
    Set PayDate5 = Sheets("KRONOS").Range("$AK:$AK") 
    Set PMethod5 = Sheets("KRONOS").Range("$AL:$AL") 

    Set PAmount6 = Sheets("KRONOS").Range("$AN:$AN") 
    Set PayDate6 = Sheets("KRONOS").Range("$AP:$AP") 
    Set PMethod6 = Sheets("KRONOS").Range("$AQ:$AQ") 

    Set PAmount7 = Sheets("KRONOS").Range("$AS:$AS") 
    Set PayDate7 = Sheets("KRONOS").Range("$AU:$AU") 
    Set PMethod7 = Sheets("KRONOS").Range("$AV:$AV") 

    Set PAmount8 = Sheets("KRONOS").Range("$AX:$AX") 
    Set PayDate8 = Sheets("KRONOS").Range("$AZ:$AZ") 
    Set PMethod8 = Sheets("KRONOS").Range("$BA:$BA") 

    Set PAmount9 = Sheets("KRONOS").Range("$BC:$BC") 
    Set PayDate9 = Sheets("KRONOS").Range("$BE:$BE") 
    Set PMethod9 = Sheets("KRONOS").Range("$BF:$BF") 

    Set PAmount10 = Sheets("KRONOS").Range("$BH:$BH") 
    Set PayDate10 = Sheets("KRONOS").Range("$BJ:$BJ") 
    Set PMethod10 = Sheets("KRONOS").Range("$BK:$BK") 
End Sub 

然後你的函數:

Public Function BANKING1(rev_date As Date) As Variant 
    Application.Volatile True 

    With New Class1 
    BANKING1 = Application.WorksheetFunction.SumIfs(_ 
       .PAmount1 _ 
       , .Team, "<>9" _ 
       , .Vstatus, "<>rejected", .Vstatus, "<>unverified" _ 
       , .Excl_Rev, "<>1" _ 
       , .PMethod1, "<>Credit" _ 
       , .PMethod1, "<>Amendment" _ 
       , .PMethod1, "<>Pre-paid" _ 
       , .PMethod1, "<>Write Off" _ 
       , .First_PD, rev_date) 
    End With 

End Function 

所有在您​​不是列出這些領域將需要以及列爲Public

或者,Class1的實例可以存儲在工作表中的全局變量中,因此您不必每次都創建新實例,但是Excel隨後會喜歡忘記全局引用。

+1

...或在函數中使用靜態變量來保存對Class1實例的引用 – 2011-06-14 18:07:46

+1

我討厭Excel忘記全局變量的方式。 – 2011-06-14 20:29:40