gpt4 book ai didi

excel - 基于行值中最后一个单元格的 Autolnumber

转载 作者:行者123 更新时间:2023-12-03 03:16:49 25 4
gpt4 key购买 nike

如果 B 列有值,以下代码将在 A 列中添加自动编号,我想更改该代码,以便它开始添加从最后一行向下计数的自动编号。 为什么?因为如果我删除第一列中已有数字的任何行,即。删除第 5 行和第 6 行后,我在第一列 1, 2, 3, 4, 7, 8 中有序列。运行代码后,它会将其更改回 1, 2, 3, 4, 5, 6,但如果它可以从最后一行开始,它会/应该看起来像这样:1、2、3、4、7、8、9、10 等。换句话说,它不应该更改现有行,而只是继续对新添加的行进行编号。我该怎么做?

Sub AutoNumber()
Dim lastRow As Long
lastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("A1").AutoFill Destination:=Range("A1:A" & lastRow), Type:=xlFillSeries
End Sub

最佳答案

对于顺序系列,我更喜欢 DataSeries 而不是 AutoFill。

Sub AutoNumber()
Dim lastRowA As Long, lastRowB As Long
lastRowA = Range("A" & Rows.Count).End(xlUp).Row
lastRowB = Range("B" & Rows.Count).End(xlUp).Row
Range(cells(lastrowa, "A"), cells(lastrowb, "A")).DataSeries _
Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, Step:=1, Trend:=False
End Sub

关于excel - 基于行值中最后一个单元格的 Autolnumber,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53018595/

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