VBA Insert row instead of copy

0 votes

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.

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. The code works fine if I wanted to just copy, but I need it to insert and shift the remaining rows below it, down.

Jan 14, 2023 in Others by Kithuzzz
• 38,010 points
378 views

1 answer to this question.

0 votes

Try this:

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
answered Jan 14, 2023 by narikkadan
• 63,420 points

Related Questions In Others

0 votes
1 answer

Copy the time to the next row of another column

Try this: Sub CopyCurrentTime() Dim currentRow As Integer currentRow = ...READ MORE

answered Jan 30, 2023 in Others by narikkadan
• 63,420 points
313 views
0 votes
1 answer

How do I copy a specific range of cells after I use AutoFilter in VBA?

Set the range of filtered data Set Rng ...READ MORE

answered Feb 2, 2023 in Others by narikkadan
• 63,420 points
402 views
0 votes
1 answer

VBA - user input of row value to use in a cell range

Use this: Range("C" & c & ":I" & ...READ MORE

answered Feb 7, 2023 in Others by narikkadan
• 63,420 points
245 views
0 votes
1 answer

Excel VBA: Obtain the Column and Row of current element in "For Each" loop

Try this: MsgBox rng.Address(RowAbsolute:=False, ColumnAbsolute:=F ...READ MORE

answered Feb 14, 2023 in Others by Kithuzzz
• 38,010 points
736 views
0 votes
1 answer

How to insert a picture into Excel at a specified cell position with VBA

Try this: With xlApp.ActiveSheet.Pictures.Insert(PicPath) With ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,420 points
1,931 views
0 votes
1 answer

Insert multiple columns to the right in sequential order

Duplicate Columns Why these complications? Flexibility. You don't have ...READ MORE

answered Feb 4, 2023 in Others by narikkadan
• 63,420 points
249 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
877 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,183 views
0 votes
1 answer

How To Copy/Cut Row of Data Based on TRUE/FALSE Condition [Excel VBA]

Solution Loop through the rows on the Price ...READ MORE

answered Feb 4, 2023 in Others by narikkadan
• 63,420 points
603 views
0 votes
1 answer

How to insert a new row in the specified cell only, of Excel sheet using c#?

I have this worksheet with a matrix ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,420 points
1,802 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP