Forum Discussion
Excel Sumif with Changing Criterion
Okay I have adjusted the code to the following:
Dim Lr, Lc As Long
Worksheets("Sheet 1").Activate
Lr = Cells(Rows.Count, "A").End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
Range("A2").Select
For a = 2 To Lr
Cells(a, 2).Value = Application.WorksheetFunction.SumIf(Worksheets("Sheet 2").Range("A1:A" & Lc), ActiveCell.Value, Worksheets("Sheet 2").Range("Lc:Lc"))
With ActiveCell.Offset(1, 0)
End With
Next a
My problem now is the sum range - if I use Range("Lc:Lc"), I get an application or object-defined error. But as the Lc may change depending on the input file, I can't set the column I would like to add in stone. However my assumption is that the sumrange will always be the last column in any given worksheet. How should I go about identifying the sumrange here?