Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

excel - Sumif across two worksheets

I am trying to add up various staff members costs on individual projects (contained in a separate worksheet called "Timesheet Entry") via a sumif (i.e. if the project number in the left hand column matches the timesheet entry then add up all of my staff costs for it, then do the next project, and so on.

The code is almost working! but returning 0 as the sum against each project.

With Worksheets("Cost Data Summary")

    Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
   
        For i = 2 To Lastrow
        .Cells(i, 2).Value = Application.SumIf(Worksheets("Timesheet Entry").Range("A2:F50"), "=.Cells.Value(i, 2)", Worksheets("Timesheet Entry").Range("F2:F50"))
        
        Next i

End With

I can only assume that I am somehow referencing the criteria incorrectly. I want it to reference the text in the cell to the left of where the sumif answer goes.

Any help would be very much appreciated.

question from:https://stackoverflow.com/questions/65557856/sumif-across-two-worksheets

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try replacing

"=.Cells.Value(i, 2)"

with

.Cells.Value(i, 2)

When enclosed in quotes, the code turns into a string literal. You shouldn't need the = sign for the argument.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...