+ Reply to Thread
Results 1 to 5 of 5

Copy one column, but loop through a second column multiple times depending on first column

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

    Copy one column, but loop through a second column multiple times depending on first column

    Hi VBA Gurus,

    I have a VBA programming project that I’ve been working on but just can’t seem to solve. Suppose I have this toy worksheet:


    Column B has a list of girls, Column C has a list of chores and, between #ALL_GIRLS tags, there are some cells which will be copied later. My code must read through Column C, copying its contents to Column D. But once it encounters an opening #ALL_GIRLS tag, it has to stop and for each girl listed in Column B, it has to copy the between-tags cells, substituting a girl’s name for the “XXXXX” placeholder:


    (The highlighting is not necessary, I’m just using it to, well, highlight the altered text.) Also, note that the #ALL_GIRLS tags are not copied.

    The #ALL_GIRLS tags may appear multiple times and anywhere in Column C:


    Again, the highlighting is just to illustrate the altered text between Columns C and D.

    Some other minor notes: The girls will always be listed starting at $B$2, and there will always be at least one girl. Columns C and D will always start at $C$2, and $D$2, respectively. We can assume Columns B and C will never grow past 100 cells, but Column D might be very, very long, depending on how many #ALL_GIRLS tags there are.

    Here’s what I have:

    Please Login or Register  to view this content.
    Obviously, this comes no-where near close to getting the job done, and I’m really stuck in the middle of that “If checkStr > 0” loop. (I do have code for this part, but it works very poorly and is far too long to post.) I’m clearly in over my head.

    Can anyone recommend a better approach? There’s got to be a succinct way of doing this in one on-the-fly pass through Column C. (I am using Excel 2019)

    FULL DISCLOSURE: I posted a similar problem on this forum, which I’d hoped would give me enough help to crack this problem. (Unfortunately, I am forbidden to post the link.) But my code gets tripped up with the tags and the copying, and I realized I needed to repost my problem, this time with more specifics.
    Attached Images Attached Images

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,208

    Re: Copy one column, but loop through a second column multiple times depending on first co

    As I don't have a sample file to work with...Give this a go...Works with setup where info starts in B2 as per images attached...
    Please Login or Register  to view this content.
    ALSO...PLEASE POST THE LINK OF THE OTHER THREAD...
    Attached Files Attached Files
    Last edited by sintek; 09-07-2019 at 07:16 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

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

    Re: Copy one column, but loop through a second column multiple times depending on first co

    Wow, thanks Sintek,

    I'm about to try your code, it looks great.

    Because I'm a new member of the forum, I'm forbidden to post URLs. So I can tell you the previous post was: excelforum.com ...and then... excel-programming-vba-macros/1288921-loop-through-two-data-ranges-at-the-same-time.html#post5189223

    Sorry, you'll have to remove the spaces I added to pull up the link.

    Thanks!

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

    Re: Copy one column, but loop through a second column multiple times depending on first co

    Yes! This worked great! I just stress-tested your code with my "real" data - 100 girls, 100 tasks, with 35+ #ALL_GIRLS tags embedded within the data. Then verified the results. It works awesome!

    My solutions were over 200 lines in code and kept inviting bugs. Your code is shorter than my variable declaration section! You also use a lot of functions I've never used before, but will look up and add them to my toolbelt. I always learn so much when someone like you takes the time to show me their code.

    Thanks, I owe you one!

  5. #5
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,208

    Re: Copy one column, but loop through a second column multiple times depending on first co

    Glad I could assist..Tx for rep +...Please remember to mark thread as solved...

+ 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. [SOLVED] Copy column range in the same column multiple times based on cell value
    By PaulM100 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-15-2019, 06:22 AM
  2. loop through range to copy row if data exists in column, then delete the column
    By wpryan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2015, 05:03 PM
  3. Replies: 2
    Last Post: 04-28-2014, 05:38 PM
  4. Replies: 3
    Last Post: 02-19-2014, 10:47 AM
  5. Copy and Paste data in column A number of times displayed in column B
    By anthony232 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-15-2013, 05:13 PM
  6. Replies: 2
    Last Post: 09-04-2013, 07:59 PM
  7. Replies: 1
    Last Post: 05-06-2011, 03:08 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