简体   繁体   English

将最后一行号添加到 VBA 中的 Excel 公式

[英]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.引用将在 SUMIF(R[5] 和 R[6] 语句中。我想更改 R 以反映最后一行是什么。

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.编辑:所以我们的想法是让公式查看标有 RD/SFC 的列和任何匹配的内容,例如,如果在 RD/SFC 行中匹配 6605,我们然后将 Est FNL sales 列中的所有内容添加到行的末尾$Ret Cost 列也是如此。 Currently trying to do that through the formula and put it in Column O.目前正试图通过公式做到这一点并将其放入 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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