开发者

Need Dynamic data range for sumif formula in Excel

开发者 https://www.devze.com 2023-02-21 22:20 出处:网络
I have a question related to excel sumif formula. I have two columns EmpID and TimeSpent(in hr.). I want to calculate the sum with sumif formula through macro. I have applied the below formula:

I have a question related to excel sumif formula. I have two columns EmpID and TimeSpent(in hr.). I want to calculate the sum with sumif formula through macro. I have applied the below formula:

=SUMIF($A$2:$A$30000,A2,OFFSET(Data!$B$2,0,0,COUNTA(Data!A:$A),2))

EmpID          TimeSpent     Sum
393               6.34       46.75
393     开发者_JAVA技巧          0.07       46.75
393              40.34       46.75
888               0.02        0.02
405              15.39       48.16
405              32.50       48.16 
405               0.27       48.16
328               5.22        63.6
328               2.08        63.6
328              25.04        63.6
328               0.11        63.6
328               0.08        63.6
328              22.34        63.6
328               0.07        63.6
328               0.06        63.6
328               7.50        63.6
328               0.20        63.6
328               0.41        63.6
328               0.49        63.6
61               18.02       36.36 
61               18.34       36.36

It shows the sum correctly to selected rows.Every file has different rows. My question is how can I set the dynamic data source to apply the sumif function. I have searched about it but no one could help me.


Would this work? I'm assuming the relevant data is always in the same columns and that the first record is always in Row 2.

Sub asdf()

    Dim rc As Integer
    Dim strFormula As String

    rc = Range("A1", Range("A1").End(xlDown)).Count

    strFormula = "=SUMIF($A$2:$A$" & rc & ",A2,OFFSET($B$2,0,0,COUNTA($A$2:$A$" & rc & "),2))"

    Range("C2") = strFormula
    Range("C2").AutoFill Destination:=Range("C2:C" & rc)

End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消