简体   繁体   English

在VBA(Excel)中,列A如何插入ID标头,然后再插入1-X UNTIL,使其到达表示数据最高行的空单元格?

[英]In VBA (Excel) how can Column A insert an ID header followed by 1-X UNTIL it reaches the empty cell representing the highest row with data?

I am trying to create a macro that will insert a column, which is to be the first column in the spreadsheet (A) while shifting all original columns over 1 column to the right. 我正在尝试创建一个宏,该宏将插入一列,这将是电子表格(A)中的第一列,同时将所有原始列向右移一列。

I then need this first column to create the header "ID" with each one numerically counting the rows: 然后,我需要第一列来创建标题“ ID”,并用数字对行进行计数:

[A] [一种]

ID ID

1 1个

2 2

3 3

I only want the numbering to stop once it has reached the last relevant row in the spreadsheet. 我只希望编号在到达电子表格中的最后相关行后停止。 I was able to generate the following VBA by doing what I would normally do to accomplish this task while recording the macro and ended up with this: 通过执行通常在记录宏的同时完成此任务的工作,我能够生成以下VBA,并最终完成了此工作:

Sub InsertID()
'
' InsertID Macro
' Add first column to be 1-##
'
' Keyboard Shortcut: Ctrl+Shift+N
'
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "ID"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "2"
    Range("A2:A3").Select
    Selection.AutoFill Destination:=Range("A2:A522")
    Range("A2:A522").Select
    Range("A1").Select
End Sub

Obvoiusly, this doesn't work for my situation. 不幸的是,这不适用于我的情况。 The template I was using only had 521 rows. 我使用的模板只有521行。 This number is going to be a variable which can usually be determined by the number of rows in the original column A (Which is now column be after running this macro). 该数字将是一个变量,通常可以通过原始列A中的行数来确定(现在,该列在运行此宏之后)。

I have extensively looked into how to create a variable for number of rows in a specific column but have been unable to find a question that has similar enough parameters even though it seems so simple. 我已经广泛研究了如何为特定列中的行数创建变量,但是即使看起来很简单,也无法找到具有足够相似参数的问题。

Thanks in advance 提前致谢

Try this... 尝试这个...

Sub CreateIDColumn()
lr = ActiveSheet.UsedRange.Rows.Count
Columns(1).Insert
Range("A1").Value = "ID"
Range("A2:A" & lr).Formula = "=ROW()-1"
Range("A2:A" & lr).Value = Range("A2:A" & lr).Value
End Sub

I believe the following code will do what you want to do. 我相信以下代码将完成您想做的事情。 It declares variables (in case Option Explicit is declared - which it should be), inserts the column, finds the last row (if there is one), and inserts relevant data. 它声明变量(如果声明Option Explicit,则应为该变量),插入列,找到最后一行(如果有的话),并插入相关数据。

Private Sub InsertID()
    Dim lastrow, i As Integer 'declaring variables

    'adding column
    Range("A1").EntireColumn.Insert

    'getting last row #
    If Application.WorksheetFunction.CountA(Cells) <> 0 Then
        lastrow = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    Lookat:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
    Else
        lastrow = 1
    End If

    'setting value of cell A1
    Cells(1, 1) = "ID"

    'setting value for the rest of the cells in column A
    For i = 2 To lastrow
        Cells(i, 1) = i - 1
    Next

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM