简体   繁体   English

VBA 插入行而不是复制

[英]VBA Insert row instead of copy

I'm trying to run before I can crawl.我想在爬行之前先跑。 I have pieced together this code, but I need it to Insert at row 24, not copy.我拼凑了这段代码,但我需要它在第 24 行插入,而不是复制。

Dim sh4 As Worksheet, sh5 As Worksheet, lr As Long, rng As Range
Set sh4 = Sheets("est")
Set sh5 = Sheets("gaf letter")
lr = sh4.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh4.Range("a1:a" & lr)
rng.EntireRow.Copy sh5.Rows("24:24")

I've attempted using.Insert, but it comes up with Method Insert of object Range Failed.我试过使用.Insert,但它出现了 object Range Failed 的 Method Insert。 The code works fine if I wanted to just copy, but I need it to insert and shift the remaining rows below it, down.如果我只想复制,代码工作正常,但我需要它来插入和移动它下面的剩余行,向下。

Option Explicit ' declare all variables

Sub InsertRows()

    Dim sh4 As Worksheet, sh5 As Worksheet
    Dim lr As Long, rng As Range
    Set sh4 = Sheets("est")
    Set sh5 = Sheets("gaf letter")
    
    Application.ScreenUpdating = False
    With sh4
        lr = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set rng = .Rows("1:" & lr)
        rng.Copy
        sh5.Rows(24).Insert shift:=xlDown
    End With
    Application.ScreenUpdating = True
    Application.CutCopyMode = False

End Sub

just go只是 go

With Sheets("est")
    .Range("A1", .Cells(.rows.Count, 1).End(xlUp)).EntireRow.Copy
    Sheets("gaf letter").rows(24).Insert shift:=xlDown
    Application.CutCopyMode = False
End With

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

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