+ Reply to Thread
Results 1 to 9 of 9

Variable Range Selection

  1. #1
    Registered User
    Join Date
    02-18-2009
    Location
    London, England
    MS-Off Ver
    Excel/Word/PPT 2003/2007
    Posts
    17

    Variable Range Selection

    Hello and sorry if my question has been discussed again in past threads,

    I have some data say in cells A1:C15. I want to use a macro which copies and pastes the data per row somewhere else in the spreadsheet. For example,

    Copy a1:c1 and paste it at e1,
    copy a2:c2 and paste it at e2...

    etc...
    up to row 15 where the data end. I know that this could be done using a range variable and a loop from 1 to 15, but i don't know how to select various ranges for each loop.. something like " for i = 1 to 15, cells (i ,1 : i ,3).select" ??

    thanks for any help

  2. #2
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Variable Range Selection

    You could probably select the first row of cells that you want to copy and then in your macro say:

    Please Login or Register  to view this content.
    to let it know how many columns it should be copying on each iteration.



    If you want to make the number of rows variable as well and not limit yourself to 15 you could select the entire range of cells and then use:

    Please Login or Register  to view this content.


    Instead of using R1C1 format you can also say:

    Please Login or Register  to view this content.
    Let me know if this is not clear.
    Last edited by jerseyguy1996; 02-01-2010 at 11:28 AM.

  3. #3
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Variable Range Selection

    Hi,

    @jerseyguy1996: if user selects the range that will be copied, it doesn't make sense to use macro to copy it - just use standard copy/paste interface offered by excel. also it is "bad" practice to select objects (i.e. using selection object) you are working with - it makes the macro work slowly. There are very little occasions when one will use selection.

    @johngr55:
    if I understand your problem you will have varying number of rows and you want to copy range starting from row 1 and including all rows of data. Is that correct? If there is no special reason it is not necessary to copy row by row. Here is the macro that will copy the range at once (You can change macro to fit your needs):

    Please Login or Register  to view this content.
    Let me know if this is what you want or if you want to copy row by row.

    Buran

    If you want
    If you are pleased with a member's answer then use the Star icon to rate it.

  4. #4
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Variable Range Selection

    Can you attach a sample workbook so that we will understand exactly what you are trying to do?

  5. #5
    Registered User
    Join Date
    02-18-2009
    Location
    London, England
    MS-Off Ver
    Excel/Word/PPT 2003/2007
    Posts
    17

    Re: Variable Range Selection

    Thanks for your answers but maybe I wasn't clear enough. The data i have look like this:

    ColA ColB ColC
    Row1 id1 5 3/3/09
    Row2 id1 7 4/3/09
    Row3 id2 8 5/3/09
    Row4 id2 1 11/3/09
    Row5 id3 11 6/3/09
    Row6 id3 -5 20/3/09

    first i have the id variable and then some other values (eg values in col. B, dates etc.)
    I want to bring the data format into the following one:

    id1 5 3/3/09 7 4/3/09
    id2 8 5/3/09 1 11/3/09
    id3 11 6/3/09 -5 20/3/09

    I need to skip the row when it is the first appearance of an id and then for each of the following rows with the same id, I need to cut the whole row and paste it next to the first occured id.

    I know how the mechanism is going to work and I have found a macro which does it, I am not sure though of how saying: when the row is correct -> copy range "row i / column A to row i / column C". In a few words, I don't know how to select the variable range from col.A to C but always depending on the id factor, and not just selecting the whole range from A1 to C6 for example.

    I hope it's more clear now.
    Thanks
    John
    Last edited by johngr55; 02-02-2010 at 10:00 AM.

  6. #6
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Variable Range Selection

    So for all id1's you want to have them in the same row and likewise for all id2's?

    Can't this just be done with a pivot table?
    Last edited by teylyn; 02-02-2010 at 03:42 PM. Reason: quote removed

  7. #7
    Registered User
    Join Date
    02-18-2009
    Location
    London, England
    MS-Off Ver
    Excel/Word/PPT 2003/2007
    Posts
    17

    Re: Variable Range Selection

    I am not sure how you can bring all the data for id1 for example in the same row using a pivot table. I think pt show you statistical measures not the actual data.. I might be missing something though ..

    regardless of what I am trying to do.. if i wanted for example to copy cases where the row is even number or where the id = "john" do you know how can the range variate in the macro? if it was for example just 1 cell in A1 I would have said " cells(x,1).select " when the condition was met in row x. if I have 3 columns A-C how can I write: " cells( x1 : x3 ).select ??

    thanks

  8. #8
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Variable Range Selection

    Will the id's all be grouped together? In other words will all of the "Johns" be grouped (regardless of whether there are 2 rows or 3 rows or will the "Johns" be separated by a "Bill" or something? Really the best way to crack this is to upload a sample copy of your spreadsheet so that we can get a good idea what you are trying to do. If you are worried about confidentiality just make up a sample spreadsheet using ficticious data.

    P.S. you will probably get a spreadsheet back that contains working code that you can then just import into your actual data spreadsheet.
    Last edited by teylyn; 02-02-2010 at 03:42 PM. Reason: quote removed

  9. #9
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Variable Range Selection

    jerseyguy, please don't quote whole posts. It's just clutter.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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