+ Reply to Thread
Results 1 to 5 of 5

Copy/paste user-selected range to/from array

  1. #1
    Registered User
    Join Date
    10-24-2014
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    6

    Copy/paste user-selected range to/from array

    I'm not sure an array is necessarily the best way to do this, so I'll try to explain what I'm trying to do, then how I'm trying to do it...

    So on Worksheet 1, user has a pivot table with a list of models:
    Model Info More info
    Apple101 Some info about this model Some other info about this model
    Apple103 ... ...
    Banana105 ... ...
    Celery105 ... ...
    Orange99 ... ...
    Lettuce31 ... ...


    On Worksheet 2, a series of formulas provides profitability info on whatever models the user chooses to include.
    Model Cost Sell Profit
    Apple101 100 (defined by VLOOKUP formula) 125 (manually defined by user) 25 (formula)
    Apple103 50 55 5
    Celery105 75 125 50
    etc etc etc etc

    The user must be able to copy some models of interest from the list in worksheet 1, and add them, with relevant formulas, to worksheet 2.

    E.g. user goes to Worksheet 1, highlights and copies cells "Orange99" and "Lettuce31". User goes to Worksheet 2, selects the next available row to place them in and pushes a button. The result is that this gets appended to Worksheet 2:
    Orange99 200 (defined by VLOOKUP formula) (to be manually defined by user) 5 (formula)
    Lettuce31 100 (defined by VLOOKUP formula) (to be manually defined by user) 125 (formula)

    From the user's point of view it's similar to a copy-paste, except rather than "CTRL-C, select cell in Worksheet 2, CTRL-V" the user doing "CTRL-C, select cell in Worksheet 2, hit Macro button". The effect is it 'pastes' in the copied models and populates the formulas in each row.

    I was trying to do this through an array:
    - User manually copies cells in Worksheet 1
    - User goes to Worksheet 2 and selects cell in row to place copied models
    - User hits button to trigger macro
    - Macro creates array based on copied cells in Worksheet 1
    - Macro loops through array; for each model in the array it populates rows in Worksheet 2 as:
    Array(1) formula blank formula
    Array(2) formula blank formula
    Array(n) formula blank formula

    The way I understand it, I need to define the array's as the number of cells in the user's selection (it may be 1 model, it may be 50), so I was using selection.count to define the array size. The problem is that when the user goes to Worksheet 2 and selects a cell they'd want to "paste" the models into, it changes selection.count to 1 (i.e. the currently active cell).
    So: is there a way to differentiate between "copied" cells and "selected" cells? How can I define the array size based on 'number of cells copied' instead of selection.count?

    Or am I making this way more complicated than it needs to be?

    Thanks!

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Copy/paste user-selected range to/from array

    You shouldn't have to count the cells.

    When you paste the values, Excel will select the total paste range. From there, you can just loop through selection:


    Please Login or Register  to view this content.
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Registered User
    Join Date
    10-24-2014
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    6

    Re: Copy/paste user-selected range to/from array

    Thanks! This works great for appending to the bottom of the Worksheet 2 list. I'd forgotten that PasteSpecial redefines the Selection, so that essentially lets me do the same thing as the array would have.

  4. #4
    Registered User
    Join Date
    10-24-2014
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    6

    Re: Copy/paste user-selected range to/from array

    Follow-up question: is there a way to insert blank rows while a selection is copied? When I use ActiveCell.Offset(0).EntireRow.Insert, it inserts the number of rows I want, but fills all cells with the copied information.

    I'm trying this:
    Please Login or Register  to view this content.
    The aim is to let the user append the copied models list anywhere in the existing list without overwriting any existing data (new data should just push all existing data down)

  5. #5
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Copy/paste user-selected range to/from array

    I've isolated the issue to the first two lines.

    Please Login or Register  to view this content.
    The second line is copying the clipboard in every cell in the insert. I'm testing for a workaround, maybe something along the lines of copying to the clipboard to another variable.

+ 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. Copy range and paste depending on the selected checkbox
    By mackypogi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-30-2014, 10:38 PM
  2. [SOLVED] macro to paste in a user selected range
    By TimlmiT in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2013, 11:35 AM
  3. Need code to COPY SAME range to a USER-SELECTED destination that changes constantly
    By redpoodles in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2013, 09:19 PM
  4. [SOLVED] copy-paste-selected-cell-range-another-sheet
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 04-26-2013, 06:12 AM
  5. copy paste selected range from one sheet to another
    By sivdin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-10-2011, 06:57 AM

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