+ Reply to Thread
Results 1 to 5 of 5

Read last element of Split array and then discard it and resize the array.

  1. #1
    Registered User
    Join Date
    12-07-2012
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    81

    Read last element of Split array and then discard it and resize the array.

    Hi,

    I'm sure this is trivial to you, but I would like to change some existing code and can't work out how.

    Column 2 of my sheet has thousands of cells, each of which currently has three elements, space -delimited.

    The code below works fine for splitting it as needed.

    Please Login or Register  to view this content.
    Each cell looks like this: oranges apples pears

    The first element stays in Col2, the second goes to Col3, while the final element skips one and goes to Col5.

    However, I now need to modify the cells to include a final (4th) element, which will be a number.

    So now it might look like : oranges apples pears 17

    I therefore need to modify the code to do the following:

    1. Read the number and put it in Col 20 of the same row.
    2. Physically remove the number from the original cell text itself, leaving it only in Col20.
    3. Discard it and re-size the array so it goes back to 3 elements again before the For loop, so the original 3 elements can be processed as previously.


    Any help would be greatly appreciated.

    PS. If anyone can tighten the code up that would also be appreciated. It has to go through 1000's of them!

    Regards.

  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
    44,447

    Re: Read last element of Split array and then discard it and resize the array.

    I'd be inclined to use Text to Columns to separate all the cell data into columns and then insert and/or delete columns to put the separated data into the columns you want it. You can then use SUBSTITUTE to remove the data in column 20 from the original cell.

    I suspect this would be a lot quicker than looping through the rows.

    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-07-2012
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    81

    Re: Read last element of Split array and then discard it and resize the array.

    Hi and thanks for responding.

    The trouble is that this will have to be done many times, since many columns are involved. The actual time taken doesn't matter particularly, since this is all being done to format various columns of thousands of rows, so I'd much rather have a modification to the code, so that I can just run it and wait!

    Thanks again.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Read last element of Split array and then discard it and resize the array.

    You don't need to resize the array, just don't loop to the last element.
    Please Login or Register  to view this content.
    To put the last element in column 20.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    12-07-2012
    Location
    London, England
    MS-Off Ver
    Office 2010
    Posts
    81

    Re: Read last element of Split array and then discard it and resize the array.

    Well I'll be damned!

    Worked perfectly. Thanks a million Norie. Nothing like a bit of lateral thinking. I feel rather stupid now. Time for some beer methinks. Thanks again.

+ 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