+ Reply to Thread
Results 1 to 8 of 8

Insert (paste) multiple rows below a user-selected cell

  1. #1
    Registered User
    Join Date
    02-18-2021
    Location
    Basel
    MS-Off Ver
    365
    Posts
    12

    Insert (paste) multiple rows below a user-selected cell

    Dear Excel Forum community,
    I am VBA beginner user and I am struggling with the following problem.
    Basically, I need to copy all rows (variable number of rows, hence I am using the LastRow function) from Sheet2 and insert them below a user-selected cell (via InputBox) in Sheet1.

    I wrote the following but it basically just adds and empty row.
    However, if the destination range is fixed, the same macro would work
    Please Login or Register  to view this content.
    Any help would be greatly appreciated!
    Best,
    Silvano
    Attached Files Attached Files
    Last edited by AliGW; 10-10-2023 at 03:44 AM. Reason: Code tags added - please review the forum guidelines.

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,676

    Re: Insert (paste) multiple rows below a user-selected cell

    PHP Code: 
    Sheets("Sheet2").Range("A2:A" LastRow).EntireRow.Copy
    Set myReply 
    Application.InputBox(prompt:="Please any Stage cell: resupply stages will be added ABOVE this cell"Type:=8)
    myReply.EntireRow.Select 
    Because between the two copy/paste commands, an input box inserted in between has disrupted everything.
    PHP Code: 
    Set myReply Application.InputBox(prompt:="Please any Stage cell: resupply stages will be added ABOVE this cell"Type:=8
    I don't know what you will enter into the input box, but I suggest entering the row number you want to insert. For example: 5.
    The entile code should be:

    PHP Code: 
    Sub Test()
    Dim LastRow As LongR
    LastRow 
    Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count"A").End(xlUp).Row
    Application.InputBox(prompt:="Please any Stage cell: resupply stages will be added ABOVE this cell")
    If 
    Not IsNumeric(R) Or <= 0 Then
        MsgBox 
    "Invalid row number! Try again"
        
    Exit Sub
    End 
    If
    Sheets("Sheet2").Range("A2:A" LastRow).EntireRow.Copy
    Sheets
    ("Sheet1").Select
    Rows
    (":" R).Select
    Selection
    .Insert Shift:=xlDown
    End Sub 
    Attached Files Attached Files
    Quang PT

  3. #3
    Registered User
    Join Date
    02-18-2021
    Location
    Basel
    MS-Off Ver
    365
    Posts
    12

    Re: Insert (paste) multiple rows below a user-selected cell

    Hi Quang!
    First and foremost thank you so much for your help: your code works perfectly!
    If you don't mind me asking: why there is so much of a difference between entering the row number (your code) and asking the user to select a cell (from which the row number could be calculated)?
    Again, this is just out of curiosity: since I am new to VBA I would like to better understand how it works!
    Thanks again for your help!
    Best,
    Silvano

  4. #4
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,676

    Re: Insert (paste) multiple rows below a user-selected cell

    Quote Originally Posted by zankzank View Post
    If you don't mind me asking: why there is so much of a difference between entering the row number (your code) and asking the user to select a cell (from which the row number could be calculated)?
    As I said before "Because between the two copy/paste commands, an input box inserted in between has disrupted everything"

    because you code: copy, then set range, then select, then paste.
    PHP Code: 
    Sheets("Sheet2").Range("A2:A" LastRow).EntireRow.Copy
    Set myReply 
    Application.InputBox(prompt:="Please any Stage cell: resupply stages will be added ABOVE this cell"Type:=8)
    myReply.EntireRow.Select 
    The "Set" range should be move up, then copy, then select:
    PHP Code: 
    Set myReply Application.InputBox(prompt:="Please any Stage cell: resupply stages will be added ABOVE this cell"Type:=8)
    Sheets("Sheet2").Range("A2:A" LastRow).EntireRow.Copy
    myReply
    .EntireRow.Select 

  5. #5
    Registered User
    Join Date
    02-18-2021
    Location
    Basel
    MS-Off Ver
    365
    Posts
    12

    Re: Insert (paste) multiple rows below a user-selected cell

    Got it!
    Based on your feedback I have re-written as follows:

    Sub Macro1()
    Dim LastRow As Long
    Dim myReply As Range

    LastRow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row

    Set myReply = Application.InputBox(prompt:="Please select any Stage cell: resupply stages will be added BELOW this cell", Type:=8)
    Sheets("Sheet2").Range("A2:A" & LastRow).EntireRow.Copy
    myReply.EntireRow.Insert Shift:=xlDown
    Application.CutCopyMode = False
    End Sub

    and not this code works too!
    Again, thank you so much for your help!

  6. #6
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,676

    Re: Insert (paste) multiple rows below a user-selected cell

    Quote Originally Posted by zankzank View Post
    and not this code works too!
    Does not work?
    Anyway, It works for me.

  7. #7
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,676

    Re: Insert (paste) multiple rows below a user-selected cell

    PHP Code: 

    Sub Macro1
    ()
    Dim LastRow As Long
    LastRow 
    Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count"A").End(xlUp).Row
    Dim myReply 
    As Range
    Set myReply 
    Application.InputBox(prompt:="Please any Stage cell: resupply stages will be added ABOVE this cell"Type:=8)
    Sheets("Sheet2").Range("A2:A" LastRow).EntireRow.Copy
    myReply
    .EntireRow.Select ' do you have this line? should be.
    Selection.Insert Shift:=xlDown
    End Sub 

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,860

    Re: Insert (paste) multiple rows below a user-selected cell

    Administrative Note:

    @zankzank

    We would very much like to help you with your query, however you need to include code tags around your code. I have done it for you once today - please do it yourself this time.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Guideline #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy multiple rows, prompt user after which row to insert them
    By Tsinos in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-06-2021, 08:33 AM
  2. [SOLVED] Insert multiple rows and copy paste text from different sheet based on cell Value
    By amitmodi_mrt in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-12-2021, 02:45 PM
  3. Replies: 1
    Last Post: 01-12-2021, 02:22 AM
  4. Insert rows for multiple selected rows
    By JN12345 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-25-2018, 04:18 PM
  5. [SOLVED] Paste formula from a row into multiple selected rows?
    By max3732 in forum Excel General
    Replies: 2
    Last Post: 05-26-2017, 11:25 AM
  6. [SOLVED] Insert User Selected Number of Blank Rows
    By HangMan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-07-2012, 12:33 PM
  7. Insert rows based on user input from cell value in other sheet
    By MrPisky in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-31-2011, 09:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1