+ Reply to Thread
Results 1 to 6 of 6

Trying to get row numbers using Range object to Copy range to array

  1. #1
    Registered User
    Join Date
    12-06-2013
    Location
    Erie
    MS-Off Ver
    Excel 2003
    Posts
    3

    Trying to get row numbers using Range object to Copy range to array

    I am using the Range object to copy a range of rows from a worksheet into an array. I need to be able to edit various cells in the rows from the array and use the row number to copy the changed rows back to the worksheet. Is there any way to do this with my approach or must I revert back to reading row by row into the array so that I can query the row number by active cell? I can't find how to do my approach, but can't believe there isn't an easier approach than reading row by row to determine each row.
    Last edited by williamdor; 09-03-2014 at 12:54 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,163

    Re: Trying to get row numbers using Range object to Copy range to array

    Please define
    a range of rows
    Where do they start? Where do they finish? Is that variable? How do you determine start and finish? Are you storing the entire row or just the first how ever many columns? Cell references would be useful.

    If you know where the range starts and ends and you don't decrease or increase the number of rows, it should be easy enough to put them back where you found them.

    Bear in mind that you are looking at your spreadsheet and you know what you want to do with it. We're not and don't.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    12-06-2013
    Location
    Erie
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Trying to get row numbers using Range object to Copy range to array

    TMS, I'm using a variable range: Range("B2:U" & CStr(lngRow)). So, the range start and end varies per what the worksheet contains at the time -- no finite last row. I copy the range into an array and the user can add or delete rows from the array by viewing in a corresponding listview, as well as make changes to the row's cells. It seems to me that I'll need to give up the efficiency of using the Range object and be forced to have to deal with all of this on a row by row basis, unless there's some Range object method/property that I've overlooked.

    Thanks for responding and I look forward to your advice -- Willamdor

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,163

    Re: Trying to get row numbers using Range object to Copy range to array

    I have no data and no code so, just as a quick experiment, I made this routine to populate a range, store it in an array and print out the content.

    Please Login or Register  to view this content.

    So, from the debug output, you can see the lower bounds and upper bounds of the array. And you can determine the content of each element in the array.

    I guess, in broad terms, that is what you are doing. Presumably, column B has a key field? And, maybe you delete the key field if you want to delete a row? And Redim Preserve the array to add new rows to the end. And you can amend the elements within the array.

    Whatever, for what it's worth, the row number maps to the array index plus one (+1). So, you know where stuff is. Similarly, the column number.

    If you consider that you have an input array that you work on ... however you do that ... when you're finished, you could loop through that input array and move the entries to an output array if the key field isn't blank. Then just clear the input RANGE and write the output ARRAY back to the worksheet.

    If you track how many "rows" you delete and how many you add, you can calculate the dimensions of the output array AND the range that you will write to.

    Anyway, my thoughts on the subject. Hope it helps. I see no reason why you need to operate on a row by row basis.

    Regards, TMS

  5. #5
    Registered User
    Join Date
    12-06-2013
    Location
    Erie
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Trying to get row numbers using Range object to Copy range to array

    TMS, it works great! Just like you showed. Thanks so much for providing such a clear solution and so quickly. This makes my task work logically as I believed it would with no silly workaround.

    Regards,
    Williamdor

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,163

    Re: Trying to get row numbers using Range object to Copy range to array

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ 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. using range.advancedfilter to copy data into a list object
    By mtnbiker98 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-28-2013, 02:58 PM
  2. [SOLVED] How to copy a range (including any formulas in the range) into an array?
    By jimmalk in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 11-28-2012, 01:51 AM
  3. [SOLVED] Name a dynamic range - Run-time error 1004 Method 'Range' of object'_Worksheet' failed
    By DavidBW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-03-2012, 08:00 AM
  4. Pass Array to Range Object
    By Paluee in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-21-2010, 01:44 PM
  5. Range Question / error 1004: method Range of object Worksheet has failed
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-07-2005, 10:06 AM

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