查找最后一行并在公式中使用它 [英] Finding the Last Row and using it in a formula

查看:64
本文介绍了查找最后一行并在公式中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理具有多行和多列的数据表.每次运行宏时,行数可以不同,因此我试图查找列的最后一行.

I am dealing with a sheet of data that has multiple rows ans columns. Each time the macro runs, the number of rows can be different, so I am trying to find the last row for a column.

对于最后一行,我正在尝试进行计算.例如:如果我得到的行是1200,则可以执行A1200/A2-1.我的代码应该将公式明确地粘贴到输出工作表中,并且当前(当前我必须自己把最后一行放进去).

With the last row, I am trying to do a calculation. For example: if the row I get is 1200, I can do A1200/A2-1. MY code should explicitly paste the formula in an output worksheet and currently (currently I have to put the last row myself).

问题:如何获得最后一行并将其放入公式中?我应该将其分配给变量,然后在公式中使用该变量吗?

Question: How can I get the last row and put it in a formula? Should I assign it to a variable and then use the variable in the formula?

我正在使用的代码行:

Sub Output()
Dim LastRowA As Long

LastRowA = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row

'this is my current method, it works using specific cells.
'I would like to change the D1662, for example, for a floating reference that gets the last row

Worksheets("Sheet2").Range("C2:C2").Formula = "='TIME SERIES'!D1662/'TIME SERIES'!D2-1"

End Sub

推荐答案

类似.只需从引号中删除变量即可.

Like so. Just remove the variable from the quotes.

Sub Output()

Dim LastRowA As Long

LastRowA = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row

'this is my current method, it works using specific cells.
'I would like to change the D1662, for example, for a floating reference that gets the last row

Worksheets("Sheet2").Range("C2:C2").Formula = "='TIME SERIES'!D" & LastRowA & "/'TIME SERIES'!D2-1"

End Sub

这篇关于查找最后一行并在公式中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆