+ Reply to Thread
Results 1 to 8 of 8

Loop Through Two Data Ranges at the Same Time?

  1. #1
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Loop Through Two Data Ranges at the Same Time?

    Hi VBA Jedi Masters,

    I would like to rewrite some inefficient VBA code. Suppose I have the following spreadsheet:


    I have a (overly complex) Macro which cycles through each princess in Column B. And for each princess, it prints a version of the poem in Column D, but with the "XXXXX" removed and the princess's name in its place:


    My VBA code is below. It works, and one of the things I like about it is that it is dynamic to the number of princesses. If you add or remove princesses, there will always be exactly one "text block" of the poem devoted to each listed princess. Here I'm defining "text block" as one set of "Mirror, mirror on the wall / Who is the fairest of them all? / That would be XXXXX" You get the idea.

    But my VBA is inefficient. As a "Part One" of the code, it simply counts the number of princesses, then copies exactly one text block per princess into Column D. But then, as a "Part Two," it returns to the top of Column D and for each princess, it finds the next available "XXXXX" and REPLACE()s that princess's name in its place. Worse, it goes back to the top of Column D for each princess. So if I had 100 princesses, the Macro would have to scan Column D 100 times.

    What would be ideal is if there was a way of doing Parts One and Two together, on-the-fly. In other words, as the Macro reads down the list of princesses, it generates one princess-customized text block in Column D as it goes.

    Or, failing that, I'd be happy if I could just do Part Two in one pass, meaning read Column D just once and do the string substitution of "XXXXX" for a princess's name as I traverse.

    Both of those solutions would require that I keep some kind of pointer or counter or iterator or something to Columns B and D. I've been fiddling with this all week, but my results are just awful and too painful to post here. Can anyone make any suggestions on how to approach this?

    Many thanks!

    My VBA:

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Loop Through Two Data Ranges at the Same Time?

    Try:
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop Through Two Data Ranges at the Same Time?

    It doesn't actually need VBA although you could easily code this in macro to add rhe formula

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

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

    D1:
    xxxxx
    D2 Copied down:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Loop Through Two Data Ranges at the Same Time?

    Wow, thanks Mumps1! Its a pleasure to watch you in action.

    Your code works great. I do have a question for you... Am I right that you are taking advantage of the fact that the "XXXXX" bit are the last five characters of the last string?

    I should have been more explicit in my original post. In the toy spreadsheet I used as an example, this is great, but in my larger spreadsheet that I have at work, the "XXXXX" may appear at the beginning, middle, or end of a string; you can't know in advance. Would it be difficult to modify your code to use the
    Please Login or Register  to view this content.
    function? I took a stab and immediately got lost.

    Thank you!

  5. #5
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Loop Through Two Data Ranges at the Same Time?

    Thanks Richard,

    This worked great. I'm a little confused how the SUBSTITUTE() function is working. Is there an advantage to using that over REPLACE() in this instance?

    Thanks!!!

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: Loop Through Two Data Ranges at the Same Time?

    Try:
    Please Login or Register  to view this content.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop Through Two Data Ranges at the Same Time?

    Substitute and Replace are similar but subtly different functions.

    Although it's not used here there's a 4th element to a Substitute that allows you to pick the nth occurrence of a character in a cell which is often extremely useful. Here it's not necessary since there's only one occurrence of "xxxxx".
    However because in many situations it offfers additional functionality I tend to turn to Substitute in preference to Replace.

    In this case Replace will work just as well and since you know the start position of the xxxxx in the text and of course the length you wouldn't need the xxxxx in D1.
    However if you were to change the text that contains the xxxxx you'd need to modify the Replace bit of the formula to reflect a new start point, with SUbstitute you wouldn't.

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop Through Two Data Ranges at the Same Time?

    Quote Originally Posted by redapplesonly View Post
    Thanks Richard,

    This worked great.
    My pleasure and thanks for the rep.

+ 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. For each loop only displaying last loop in all of my ranges
    By mickeyb121 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-12-2017, 06:52 AM
  2. [SOLVED] How do I use a LOOP or COUNTER to move down a column of data one row at a time?
    By Tom T Hall in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-06-2017, 11:23 AM
  3. Loop through worksheets and copy data ranges into one sheet
    By mcaballero in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-03-2015, 04:44 PM
  4. [SOLVED] loop through certain ranges and put down specific text based on previous cell with data
    By liz5818 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-02-2013, 01:43 PM
  5. [SOLVED] Counting time data within ranges
    By ly tieu long in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-27-2012, 04:20 PM
  6. Averaging Data Points Associated to Date/Time Ranges
    By Tokkul in forum Excel General
    Replies: 1
    Last Post: 01-12-2012, 05:27 PM
  7. Do While Loop Q: Copying sheet data ranges to another sheet's ranges
    By foolios in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-30-2011, 05:05 PM

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