+ Reply to Thread
Results 1 to 11 of 11

Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

  1. #1
    Registered User
    Join Date
    08-09-2017
    Location
    Indiana
    MS-Off Ver
    Execl 2013, Windows 10
    Posts
    23

    Post Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    I am still very new and can't figure out how to accomplish this. I have a spreadsheet (attached) with Names in Column B that are not uniform, some start with Last Name, some start with First Name, some have MI some do not, some have SR JR some do not.

    First, I'd like to setup VBA/macro starting on Column B Row 2, Select & Copy the field, Ask "Does XXXX start with Last Name? (ie XXXX displays the field value), If answer Yes copy the field value to Column C of the same row, then go to the next line down, If answer No copy the field value to Column D of the same row, then move to the next line down and repeat the above steps again until the last row in Column B is not populated. I have multiple spreadsheets - all with different # of rows.

    Unfortunately I have had issues getting the name of the cell to display in the message, and I am not understanding the looping through to get it to continue after the Yes or No to the next lines. I get errors ALL OVER. Can anyone guide me to get this to work?

    Second, I plan to record the text to column and split all the values, then add to the above. I think I can handle this part by recording a macro and using that to get it to work. After the above is working.


    HERE's some of the VBA I can't get to work:


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by alansidman; 11-05-2017 at 08:02 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,855

    Re: Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Valued Forum Contributor Maudibe's Avatar
    Join Date
    12-21-2016
    Location
    USA
    MS-Off Ver
    2010-2013
    Posts
    326

    Re: Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    WBravard,

    The following code will cycle through your list of names and prompt if the first XXXXX is the first or last name. You can cancel at that point or answer Yes or No. The entire name will be split and placed into the appropriate columns on that line

    HTH,
    Maud

    Please Login or Register  to view this content.
    names1.png

    MsgBoxYesNoLoopCopy.xlsm

  4. #4
    Registered User
    Join Date
    08-09-2017
    Location
    Indiana
    MS-Off Ver
    Execl 2013, Windows 10
    Posts
    23

    Re: Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    Maudibe,

    Thank you - that worked great, however I have the following questions:
    1. split is looking for space only, sometimes there is a comma delimiter, or a comma and a space - is there a way to add in both delimiters?
    2. Can you explain the Answer = 2 (will exit) and Answer = 6 (will put in Column C or D) but what is causing 2 verses a 6/what does this represent? I'm not following it exactly.
    3. How is it knowing what to put in C verses D? I see it telling it, but not getting how it knows to do that - would you be willing to explain this?
    4. Some names have a first name, middle name (more than 1 character MI ie Jennifer Kelly Harris) and one or two last names. The middle names and two last names or spaces in last name (ie: De La Ruche). Is there anything that can be input to work around these issues?

    Sorry for all my questions, I worked for days trying to get something to work - I am not a programmer so I am not fully understanding/following it all yet.

    Thanks!!! Wendy

  5. #5
    Valued Forum Contributor Maudibe's Avatar
    Join Date
    12-21-2016
    Location
    USA
    MS-Off Ver
    2010-2013
    Posts
    326

    Re: Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    Sorry for all my questions....
    But these are good questions

    1. split is looking for space only, sometimes there is a comma delimiter, or a comma and a space - is there a way to add in both delimiters? We can add the replace function to change commas to spaces
    2. Can you explain the Answer = 2 (will exit) and Answer = 6 (will put in Column C or D) but what is causing 2 verses a 6/what does this represent? I'm not following it exactly. These are the numeric constants for vbCancel/vbYes/vbNo https://msdn.microsoft.com/....
    3. How is it knowing what to put in C verses D? I see it telling it, but not getting how it knows to do that - would you be willing to explain this? The name string is split into the s array and array values are sent to the cells based on the criteria in the code
    4. Some names have a first name, middle name (more than 1 character MI ie Jennifer Kelly Harris) and one or two last names. The middle names and two last names or spaces in last name (ie: De La Ruche). Is there anything that can be input to work around these issues? I have to give this one some additional thought. Will post back

    Maud

  6. #6
    Registered User
    Join Date
    08-09-2017
    Location
    Indiana
    MS-Off Ver
    Execl 2013, Windows 10
    Posts
    23

    Re: Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    Thanks for your responses.
    So if we replace a comma with a space before the split - some may have two spaces - will that cause a space to be in front of the 2nd item in the array?
    I'll look up these constants - see what I can learn.
    Arrays - this is where I'm confused - Looks to me if it is = 2 - I'm guessing that is code for Cancel, If it is 6 that is the numeric code for YES, so ELSE is what happens when it is No - I looked up the numeric constants - thanks I think I got this now.
    I'll await for your post back on the middle name/last name issue. Right now what I am doing is I put in an extra column - just split it all out straight then manually correct the values for middle names and last names. Which using what you sent above - will really decrease the other manual stuff I have to do and save me time.

    THANKS!
    Last edited by WBravard; 11-06-2017 at 08:43 AM.

  7. #7
    Valued Forum Contributor Maudibe's Avatar
    Join Date
    12-21-2016
    Location
    USA
    MS-Off Ver
    2010-2013
    Posts
    326

    Re: Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    So if we replace a comma with a space before the split - some may have two spaces - will that cause a space to be in front of the 2nd item in the array?
    No, not really. The split function creates an array of values of what is between the split parameter. For example: "I like to code in Excel". Note the extra space between the words "code" and "in". If we applied the following Split method:
    s=Split("I like to code in Excel", " ")
    the spaces separate the array values in the following manner.
    left of the first space, s(0)="I"
    between the first and second space, s(1)="like"
    between the second and third space, s(2)="to"
    between the third and fourth space, s(3)="code"
    between the fourth and fifth space (consecutive spaces), s(4)="" What is between the 4th and 5th space? Nothing!
    between the fifth and sixth space, s(5)="in"
    To the right of the 6th space, s(6)="Excel"

    A value of "" will be ignored in the code.

    Working on a solution for the middle name issue.

    Maud

  8. #8
    Valued Forum Contributor Maudibe's Avatar
    Join Date
    12-21-2016
    Location
    USA
    MS-Off Ver
    2010-2013
    Posts
    326

    Re: Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    Wendy,

    I made some modifications to the code:
    1. Cycling through the names will start at the row of the selected cell. This will enable you to start at any row and/or pickup at any row if you clicked cancel.

    2. Replace method was added to deal with commas and periods along with an additional condition to deal with the empty array value that it would create. You can easily add additional replace statements for other punctuation by copy/pasting a new replace statement and substituting the punctuation character.

    3. If there are multiple segmented last names or middle names fuller than just an initial then click cancel in the message box and correct it manually. Resume by selecting a cell somewhere on the next row and restart the code (Alt-F8)

    There are too many unpredictable circumstances with identifying multiple segmented last and middle names so providing the ability to correct manually and resume seemed the next best workaround.

    HTH,
    Maud


    MsgBoxYesNoLoopCopy_Rev1.xlsm

  9. #9
    Registered User
    Join Date
    08-09-2017
    Location
    Indiana
    MS-Off Ver
    Execl 2013, Windows 10
    Posts
    23

    Re: Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    THANK YOU - I'll check it out and work through this - I like the cancel and fix right away option! Your help has been greatly appreciated!!

  10. #10
    Valued Forum Contributor Maudibe's Avatar
    Join Date
    12-21-2016
    Location
    USA
    MS-Off Ver
    2010-2013
    Posts
    326

    Drag and Drop names

    Wendy,

    I have come up with a cool solution to address the multiple last names and middle names issue.

    A userform opens with a label indicating the name as it appears in column B. Under it is a list box populated with the splits of the name into segments by spaces. To its right are 4 textboxes (lastname, firstname, middle, suffix)

    The user first drags the last name (or the first segment of the last name) to the lastname textbox. The code will then fill in the rest of the fields by moving the rest of the contents to the appropriate field textboxes. If it can not make the determination, then you can manually drag each remaining segment in the listbox to its matching textbox field. For multiple last names, you can drag each segment to the lastname text box where each drag will append to the contents already in place.

    Ex. Name is Helen De La Rocha

    Drag in order to the lastname textbox the segments "De", "La", then "Rocha". With the last name filled, the code will attempt to autofill the rest of the fields

    As each segment is dragged out of the listbox, the segment will be removed and only the remaining segments populate the listbox.

    When all the fields are filled, clicking the Go button transfers the name to the worksheet. If you do not agree with the order, you can reset the name and repeat the drag and drop.

    Like the previous version, the code starts at the active cell.

    HTH,
    Maud

    DragNdrop1.png

    MsgBoxYesNoLoopCopy_Rev2.xlsm

  11. #11
    Registered User
    Join Date
    08-09-2017
    Location
    Indiana
    MS-Off Ver
    Execl 2013, Windows 10
    Posts
    23

    Re: Loop through Column B msgboxYesNo if Yes copy to Column C, if No copy to Column D

    This is GREAT - THANK YOU for all of your help. I ran it and it is very smooth - will encorporate this!

+ 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. 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
  2. Replies: 1
    Last Post: 05-30-2015, 04:38 PM
  3. Replies: 5
    Last Post: 11-17-2014, 02:40 PM
  4. Copy data from column K to column B if Column C and column I match
    By goog2013 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2013, 12:19 PM
  5. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  6. [SOLVED] Loop-copy cell values from a row, and then copy them into a column
    By vukovicnikola in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-15-2012, 06:03 AM
  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