vba 范围内的最后一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12588424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:04:19  来源:igfitidea点击:

Last Row in Range

excel-vbaexcel-2010vbaexcel

提问by GoldBishop

Having an issue finding information and/or solutions that produce the desired result, so here it goes.

在查找信息和/或产生所需结果的解决方案时遇到问题,就这样吧。

Right now, and potentially at production time, i will have a "template" named-range on a worksheet. On first use this NamedRange is only rngNamed1but after the first use there could be more Named Ranges similar to this one, say up to rngNamed30.

现在,可能在生产时,我将在工作表上有一个“模板”命名范围。在第一次使用时,这个 NamedRange 只是,rngNamed1但在第一次使用之后,可能会有更多与这个类似的命名范围,比如最多rngNamed30.

Lets say on the 2nd run after building up to rngNamed30, that i need to add 3 more of the NamedRange, which results in rngNamed33.

让我们说在建立后的第 2 次运行时rngNamed30,我需要再添加 3 个 NamedRange,结果是rngNamed33.

What i need to do is basically find the last row & column of the last Named Range, so i know where to start the copying of data to and declare the next Named Range.

我需要做的基本上是找到最后一个命名范围的最后一行和最后一列,所以我知道从哪里开始将数据复制到并声明下一个命名范围。

What i have tried so far:

到目前为止我尝试过的:

Dim rng As range
Set rng = range("rngNamed1")
'Set rng = rng.Find("*", rng.Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False)
Debug.Print rng.Find("*", rng.Cells(1, 1), , , , xlPrevious).Address

The problem with most of the solutions out there is they are based on finding the last cell WITH DATA. I just need to know the Last Cell of the Range, irregardless of whether it contains data or not.

大多数解决方案的问题在于它们基于找到带有数据的最后一个单元格。我只需要知道范围的最后一个单元格,无论它是否包含数据。

回答by JustinJDavies

You can do this easily using the Row and Rows.Count of the range:

您可以使用范围的 Row 和 Rows.Count 轻松完成此操作:

Sub NextRowAfterRange()
    Dim ws As Worksheet
    Dim rangeNom As String
    Dim nextRow As Long

    ' Enter desired name here
    rangeNom = "rngName1"

    Set ws = ActiveSheet

    nextRow = ws.Range(rangeNom).Row + ws.Range(rangeNom).Rows.Count

    MsgBox nextRow
End Sub