+ Reply to Thread
Results 1 to 6 of 6

Copy a variable number of rows

  1. #1
    Registered User
    Join Date
    01-12-2020
    Location
    California
    MS-Off Ver
    2016
    Posts
    53

    Lightbulb Copy a variable number of rows

    Hi,
    I am trying to write a VBA script to copy a variable number of rows from top to bottom from sheet1 in sheet2; the number of rows copied, equal to the value of cell C2 (highlighted in yellow) in sheet2
    For example if C2=2, copy 2 rows, C2=3, copy 3 rows, etc
    Any help would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Copy a variable number of rows

    Not enough information provided. Do you want the header row from Sheet1 copied over? Does it count as 1 of the rows? i.e. C2=2 then data copied = header row + 1 row of data? WHERE on Sheet2 do you want it copied to? When the number in C2 changes do you want the previously copied data deleted/ovewritten or appended to with the next set?

    Might be an idea to show us your desired result in a couple of scenarios.

    BSB

  3. #3
    Registered User
    Join Date
    01-12-2020
    Location
    California
    MS-Off Ver
    2016
    Posts
    53

    Re: Copy a variable number of rows

    Sorry for not providing all the info.
    Header from sheet 1 does not need to be copied over
    Data rows that need to be copied start below the header; they will be copied starting with row 3 in sheet2; every time the script runs (with a new C2 value), it would copy rows by appending to the last row
    Thank you so much for your willingness to help me resolve my problem!

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Copy a variable number of rows

    Right click the tab for Sheet2 and select "View Code". Paste the code below into thet Sheet2 module then close the VB Editor.
    Changing the number in C2 (so long as it's greater than zero) will move that number of rows from Sheet1 to Sheet2.
    Please Login or Register  to view this content.
    Is that what you mean?

    BSB

  5. #5
    Registered User
    Join Date
    01-12-2020
    Location
    California
    MS-Off Ver
    2016
    Posts
    53

    Re: Copy a variable number of rows

    That's awesome and that's exactly what I mean. One more question though...
    I want to write this code as part of a sub to add as part of a macro. The way is written does not work for a macro; I think the variable needs to be defined in a different way?
    Thanks you so much for your help!

  6. #6
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Copy a variable number of rows

    Maybe this?
    Please Login or Register  to view this content.
    BSB

+ 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. VBA: Extract variable number of rows per variable number of phone numbers
    By redstone2830 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-17-2018, 09:22 PM
  2. use current cells and copy down to variable number of rows
    By dgraham62 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-20-2017, 07:47 PM
  3. [SOLVED] Copy variable number of rows
    By potga in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-27-2016, 10:40 PM
  4. Macro to copy data into a variable number of rows
    By catb05 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-10-2015, 05:19 PM
  5. Copy a variable number of rows and columns to last row of another worksheet
    By SSGMiami in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-07-2011, 10:46 AM
  6. Copy a variable number of rows
    By satishkhatri in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-23-2008, 07:52 AM
  7. Copy cells to variable number of rows
    By Acct Supr - DCTC in forum Excel General
    Replies: 3
    Last Post: 11-14-2005, 01:30 PM

Tags for this Thread

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