Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 查找最后一行并在公式中使用它_Vba_Excel - Fatal编程技术网

Vba 查找最后一行并在公式中使用它

Vba 查找最后一行并在公式中使用它,vba,excel,Vba,Excel,我正在处理一张包含多行和多列的数据表。每次宏运行时,行数可能会有所不同,因此我尝试查找列的最后一行 对于最后一行,我正在尝试进行计算。例如:如果我得到的行是1200,我可以执行A1200/A2-1。我的代码应该明确地将公式粘贴到输出工作表中,并且当前(当前我必须自己放置最后一行) 问题:如何得到最后一行并将其放入公式中?我是否应该将其分配给一个变量,然后在公式中使用该变量 我正在使用的代码行: Sub Output() Dim LastRowA As Long LastRowA = Works

我正在处理一张包含多行和多列的数据表。每次宏运行时,行数可能会有所不同,因此我尝试查找列的最后一行

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

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

我正在使用的代码行:

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

像这样。只需从引号中删除变量

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

像这样。只需从引号中删除变量

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

用户定义的函数怎么样?通过这种方式,您可以使用
=LastRow(ColumnNumber)
将其添加到excel中的方程式中。您可以将其保留在vba编辑器中,并在excel中将单元格值设置为“
=LastRowValue(“SheetName”,1)/A2-1
”,其中1将是列A

Function LastRowValue(WorksheetName As String, Col As Long) As Long
'=======================================================================
'This can be typed into an excel cell as a normal function
'"=LastRow("SheetName",ColumnNumber)" The Column Number is indexed 
'starting with A=1 so C=3 and AA=27. Enter the WorksheetName in
'quotes "WorksheetName".
'========================================================================
Dim LR As Long

'LR will find the last row in column number "col" in WorksheetName.
LR = ThisWorkbook.Sheets(WorksheetName).Cells(Rows.Count, Col).End(xlUp).Row

'LastRowValue will be the output of this function and will be the value of 
'the last row in WorksheetName.
LastRowValue = Cells(LR, Col).Value

End Function

用户定义的函数怎么样?通过这种方式,您可以使用
=LastRow(ColumnNumber)
将其添加到excel中的方程式中。您可以将其保留在vba编辑器中,并在excel中将单元格值设置为“
=LastRowValue(“SheetName”,1)/A2-1
”,其中1将是列A

Function LastRowValue(WorksheetName As String, Col As Long) As Long
'=======================================================================
'This can be typed into an excel cell as a normal function
'"=LastRow("SheetName",ColumnNumber)" The Column Number is indexed 
'starting with A=1 so C=3 and AA=27. Enter the WorksheetName in
'quotes "WorksheetName".
'========================================================================
Dim LR As Long

'LR will find the last row in column number "col" in WorksheetName.
LR = ThisWorkbook.Sheets(WorksheetName).Cells(Rows.Count, Col).End(xlUp).Row

'LastRowValue will be the output of this function and will be the value of 
'the last row in WorksheetName.
LastRowValue = Cells(LR, Col).Value

End Function

是-只需将
LastRowA
变量连接到公式字符串中:
工作表(“Sheet2”).Range(“C2:C2”).formula=“='TIME SERIES'!D”&LastRowA&“/'TIME SERIES'!D2-1”
谢谢您的回答。与下面的答案完全相同。很遗憾,我不能接受两个答案。是-只需将
LastRowA
变量连接到公式字符串中:
工作表(“Sheet2”).Range(“C2:C2”).formula=“='TIME SERIES'!D”&LastRowA&“/'TIME SERIES'!D2-1”
谢谢您的回答。与下面的答案完全相同。很遗憾,我不能接受两个答案。谢谢你的回答。我不知道,我也在做类似的事情,但出于某种原因,代码注释了“时间序列”。我不知道为什么这不会发生在你的文章中,但尽管如此,它还是起作用的。单引号是一个注释指示器,所以可能你不小心在引号中漏掉了什么。我发现,我的是“='时间序列'!D”,在=符号后面有一个空格。VBA非常挑剔!谢谢你的回答。我不知道,我也在做类似的事情,但出于某种原因,代码注释了“时间序列”。我不知道为什么这不会发生在你的文章中,但尽管如此,它还是起作用的。单引号是一个注释指示器,所以可能你不小心在引号中漏掉了什么。我发现,我的是“='时间序列'!D”,在=符号后面有一个空格。VBA非常挑剔!