简体   繁体   中英

Adding the last row number to an excel formula in VBA

having a bit of a problem and not sure how possible this is. I'm trying to get the last row number (not count) in a column and then use that number in a formula. The reference would be in the SUMIF(R[5] and R[6] statements. I want to change the R to reflect whatever the last row is.

Here is what I have and while it returns the row number I need I can't figure out how to define or use it in the formula. I'm not sure if this is even the best way to do it.

Any help in the right direction would be greatly appreciated!

Sub test()
    Dim lrow As Long

    lrow = ActiveSheet.Cells(Rows.count, 2).End(xlUp).Offset(1, 0).Select

    Selection.Value = ActiveCell.Row - 1

    ActiveCell.FormulaR1C1 = "=if(RC[-6]="""",if(RC[-8]=R[-1]C[-8],R[-1]C,SUMIF(R[5]C[-8]:R[6]C[-8],RC[-8],R[5]C[-11]:R[6]C[-11])),"""")"
End Sub

EDIT: So the idea is to get the formula to look at the column labeled RD/SFC and anything that matches, for example if in RD/SFC row match 6605 we then add everything in the columns Est FNL sales to the end of the row and same for the $Ret Cost column. Currently trying to do that through the formula and put it in Column O.

Hope that helps explain a little bit more

在此处输入图像描述

may this help:

'To get the last row with data in a given column
Public Function lastRowInOneColumn(ByVal ws As Worksheet, ByVal givenCol As Long) As Long

With ws
    lastRowInOneColumn = .Cells(.Rows.count, givenCol).End(xlUp).Row
End With

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM