Excel 按顺序添加一行

Excel 按顺序添加一行,excel,vba,Excel,Vba,我正在表格底部添加一行,使用- Dim Lr As Integer Application.ScreenUpdating = False Lr = Range("B" & Rows.Count).End(xlUp).Row 'Searching last row in column B Rows(Lr + 1).Insert Shift:=xlDown 'Inserting new row Cells(Lr + 1, "B") = Cells(Lr, "B") + 1 '

我正在表格底部添加一行,使用-

Dim Lr As Integer
Application.ScreenUpdating = False
Lr = Range("B" & Rows.Count).End(xlUp).Row 'Searching last row in column B
    Rows(Lr + 1).Insert Shift:=xlDown 'Inserting new row
    Cells(Lr + 1, "B") = Cells(Lr, "B") + 1 'Adding a sequential number
    Rows(Lr).Copy 'Copying format of last row
    Rows(Lr + 1).PasteSpecial Paste:=xlPasteFormats 'Pasting format to new row
    Application.CutCopyMode = False 'Deactivating copy mode
行中有一些公式需要复制到新行中。与

Rows(Lr + 1).PasteSpecial Paste:xlPasteFormats 
它只使用序号粘贴新行,而不使用公式。和

Rows(Lr + 1).PasteSpecial Paste:xlPasteFormulas
它只使用公式粘贴新行,但不使用序号。某个地方少了一些东西。
请帮帮我。

我相信以下内容会达到您的期望,我对您的代码做了一些调整,而且声明您正在使用的工作表始终是一个好主意,因此Excel不会假设它是ActiveSheet,而且我已将整数更改为只要Excel的行数超过整数所能处理的行数,请看下面:

Sub foo()
Dim ws As Worksheet: Set ws = ActiveSheet
'declare and set the worksheet you are working with, amend ActiveSheet to "Sheets("YourSheetName")"
Dim Lr As Long
Application.ScreenUpdating = False
Lr = ws.Range("B" & ws.Rows.Count).End(xlUp).Row 'Searching last row in column B
    ws.Rows(Lr + 1).Insert Shift:=xlDown 'Inserting new row
    ws.Rows(Lr).Copy 'Copying format of last row
    ws.Rows(Lr + 1).PasteSpecial Paste:=xlPasteFormats 'Pasting format to new row
    ws.Rows(Lr + 1).PasteSpecial Paste: xlPasteFormulas
    ws.Cells(Lr + 1, "B") = ws.Cells(Lr, "B") + 1 'Adding a sequential number
    Application.CutCopyMode = False 'Deactivating copy mode
Application.ScreenUpdating = True
End Sub

你是说这个
单元(Lr+1,“B”)=单元(Lr,“B”)+1
不工作?它是否将新单元格留空或不增加它或什么?