我一直在创建几个函数,所有函数都共享很多相同的代码。相关代码是从Dim Order_Type As Range
到Set PMethod10 = Sheets("KRONOS").Range("$BK:$BK")
。
如何将它放在全局函数或类似的东西中,以便只调用该函数而不是每次都使用整个代码?
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 :(得分:3)
您没有返回任何值,因此您不需要创建可以使用子例程的函数。但是,由于你所做的只是设置Range变量,你应该只是将它们全部命名为range,并删除所有代码。然后你会像这样引用它们:
Range("Final_Price").Value = 30
答案 1 :(得分:2)
你不能将你的变量声明为somehwere,但是你可以委托一个对象为你保存它们。
创建一个类并将此代码放入其中:
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
DIM
中未列出的所有这些字段也需要列为Public
。
或者,Class1
的实例可以存储在工作表的全局变量中,因此您不必每次都创建新实例,但是,Excel喜欢忘记全局引用。