+ Reply to Thread
Results 1 to 8 of 8

Loop & Array (Probably Simple!)

  1. #1
    Registered User
    Join Date
    01-23-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    7

    Loop & Array (Probably Simple!)

    Hi Guys,

    I have the following code which is designed to simply find and replace the values in a given range with values in another range. The find values are defined on a sheet in one range (column) and the replacement values in another. eg

    I am assigning each other those ranges as arrays in the formula. The code needs to run through array1 value 1 and replace it with array2 value 1

    I'm figuring there are two options here: Either define it all as one multi column array (as the two data sets are next to each other) or define them as separate ranges as I have (this method would seem to be good if the array ranges where in different locations)

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    This is the section of code causing issues so far, I think my issues is I'm not coding the "i" variable correctly? Is there a simple fix here?

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Thank you

  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
    47,997

    Re: Loop & Array (Probably Simple!)

    Some questions ... Does it fail immediately or at some point in the loop? What's the value of i when it fails? What value is being searched for when it fails? Is that value present? When it fails, how does it fail; what message do you get? Has anything been replaced? What range is selected when you run the code? Is it going to find all, some or none of the values to be replaced?

    You could always put an On Error Resume Next at the start and an On Error Goto 0 at the end to ignore any errors and see what happens.

    If this doesn't help to diagnose and fix the problem, please post a sample workbook with instructions to run the code.

    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
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Loop & Array (Probably Simple!)

    Please Login or Register  to view this content.
    If solved remember to mark Thread as solved

  4. #4
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Loop & Array (Probably Simple!)

    Not sure but somehow your oldlist and newlist arrays are actually taken as 2 dimensional arrays by VBA. And first and last element of array are empty (looked at Locals window)
    Below code is working but you have to stop the loop one step earlier (as i value is increased by 1 in below step and last element is empty)
    Please Login or Register  to view this content.

  5. #5
    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
    47,997

    Re: Loop & Array (Probably Simple!)

    Use Transpose to get a single dimension array.

    Please Login or Register  to view this content.

    Regards, TMS

  6. #6
    Registered User
    Join Date
    01-23-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Loop & Array (Probably Simple!)

    Great work guys all fixed,

    I knew it was something simple I was missing. I have kept both methods above:

    haripopuri; I modified yours as instructed with
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    to fix the error once it gets to the final record. Also I release this is a good method for larger arrays?, i.e I can define a single name list and then use
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    etc to define what column of the array.

    TMShucks
    Your transpose method will also come in handy!

    thanks Again!

  7. #7
    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
    47,997

    Re: Loop & Array (Probably Simple!)

    You're welcome.

  8. #8
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Loop & Array (Probably Simple!)

    Quote Originally Posted by B_B View Post
    Also I release this is a good method for larger arrays?, i.e I can define a single name list and then use
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    etc to define what column of the array.
    Yes. You can use multi-dimensional arrays (upto 60 dimensions for that matter)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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