Excel VBA:适用于单张纸的function

我有大量的具有相同布局的工作表,列G6:G38是年份和列H6:H38是相应的支付金额。 我试图创build一个简单的公式,我可以在所有通过这些列进行search的表格中使用该公式,并在给定年份中总结这些公式。 这是我写入模块的function:

 Public Function TPaid(Year As Integer) Application.Volatile Dim x As Integer x = 6 Do Until x = 38 If Range("G" & x).Value = Year Then TPaid = TPaid + Range("H" & x).Value End If x = x + 1 Loop End Function 

将公式插入到所有工作表中的单个单元格中后,我发现每次input公式时,都会导致其他工作表上相同公式的所有其他用法显示最近的计算结果。 我怎样才能使公式只适用于其input的工作表?

例如:在单元格A1 sheet1 ,我推算=Tpaid(2013) 。 然后在单元格A1 sheet2上推算=Tpaid(2013) 。 在input到工作表2上后,显示的数量是正确的,但现在工作表1中的数量已经更改为显示与工作表2中相同的数量,这是不正确的。

正如GSerg所说,这可以通过几种方式来完成,其中最好的是=SUMIF(G6:G38,2013,H6:H38)

除此以外,

 Public Function TPaid(Year As Integer) Application.Volatile Dim x As Integer, obj as Object With Application.Caller.Parent For x = 6 to 38 If .Range("G" & x).Value = Year Then TPaid = TPaid + .Range("H" & x).Value Next End With End Function