gpt4 book ai didi

excel - 选择整个最后一行

转载 作者:行者123 更新时间:2023-12-04 21:33:46 25 4
gpt4 key购买 nike

我试图让我的代码找到工作表中的最后一行并复制整行并将其粘贴到下面。目前我有:

Cells(Application.Rows.Count, 1).End(xlUp).Select
Selection.Copy
Cells(Application.Rows.Count, 1).End(xlUp).Offset(1, 0).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False

这非常适合找到行中的最后一个单元格,但是当它复制/粘贴时,它只会抓取第一个单元格。所以,我想知道您是否可以使用此代码找到最后一行,然后选择整个行,然后将其复制/粘贴到下面。请记住,我不能只输入
Rows("3:3").Select
Selection.Copy
Rows("4:4").Select
Selection.Insert Shift:=xlDown

因为行将是可变的。谢谢!
Dim lastRow As Long

lastRow = Cells(Application.Rows.Count, 1).End(xlUp).Row

Rows(lastRow).Select
Selection.Copy
Rows(lastRow).Offset(1, 0).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False

上面的代码会找到最后一行,选择它,复制它,然后粘贴到下面。但是,在某些情况下,我需要复制最后一行并将其粘贴到其下方的几行中,因此:
  Rows("3:3").Select
Selection.Copy
Rows("4:16").Select
Selection.Insert Shift:=xlDown

有这样的效果,但是我无法将选定的行粘贴到几行中,因为
 Rows(lastRow:4).Select

吓坏了。有没有办法将“16”添加到最后一行,以便复制该行并将其粘贴到接下来的 14 行中,等等?

最佳答案

您可以使用 .Resize 然后“向下填充”“选择”多行。

dim rws as long
rws = 14 'total # of rows 3:16
with worksheets("sheet1")
.cells(.rows.count, "A").end(xlup).resize(rws, .columns.count).filldown
end with

关于excel - 选择整个最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44813022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com