+ Reply to Thread
Results 1 to 21 of 21

Move Data From One Column to Another based on column headers and Row Text

  1. #1
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Move Data From One Column to Another based on column headers and Row Text

    I am in need of restructuring a spreadsheet of addresses for mailing purposes. (I tried to find something similar answered previously, but nothing seemed to work for me.)

    I have attached a small example spreadsheet below, but our spreadsheets can be hundreds or thousands of rows in length.

    I need a macro that will look for the "PO Box" addresses under the column headers named "ADDRESS2" and "ADDRESS3".
    The PO Box addresses will need to be moved under the column header "ADDRESS1" within the same row.
    It will need to overwrite the text that is already under "ADDRESS1" and delete the text from the "ADDRESS2" and "ADDRESS3" columns - UNLESS the text in 'ADDRESS2" is a PO Box AND "ADDRESS1" begins with "c/o".
    If the data in "ADDRESS2" or "ADDRESS3" is anything other than a PO Box it will remain the same. As will "ADDRESS 1".

    Basically if there is a PO Box it needs to be in the column named "ADDRESS1" and overwrite anything else that was there. The exception will be for PO Boxes that are in c/o someone else, the PO Box will then need to be listed in the column directly after the column that has c/o.

    If the c/o exception will be too difficult the code could just highlight those scenarios and we could fix them manually. We usually do not have a lot of them, but enough that we need to be mindful of them.

    The different scenarios are listed in my sample spreadsheet.

    Also, the code will have to use the column header names in row 1 because those headers are not always in the same column.

    PO Box Address Test.xlsx

    I am new to VBA so I would appreciate any help at all.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Move Data From One Column to Another based on column headers and Row Text

    Maybe:

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Move Data From One Column to Another based on column headers and Row Text

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

  4. #4
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    JOHN H. DAVIS Oh my goodness, we are so close (well, you are)! Thank you!
    It just seems to be missing one element. If "ADDRESS1" has "c/o" and the PO Box is in "ADDRESS3" it is not pulling it over to "ADDRESS2".
    ie. Row 11 and 13 in the sample spreadsheet.

    Thanks for your help!
    Last edited by DerbysGal; 02-07-2014 at 02:32 PM.

  5. #5
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    patel45 your code works for this sheet but, I do not believe your code is looking at the column headers. Those headers will not always be in those columns, I need it to look for "ADDRESS1", "ADDRESS2", and "ADDRESS3".

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Move Data From One Column to Another based on column headers and Row Text

    Try:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    JOHN H. DAVIS
    It gave me a run-time error '1004' on...
    If Cells(i, zz).Value Like "*PO*" And Left(Cells(i, xx), 3) <> "c/o" Then

  8. #8
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    It only gave me that error when I moved the columns around beforehand. It worked perfectly on the sample spreadsheet.

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Move Data From One Column to Another based on column headers and Row Text

    I'm not duplicating the error when I test on your sample. Try to re-copy the code and repaste to your module and run it again.

  10. #10
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Move Data From One Column to Another based on column headers and Row Text

    Quote Originally Posted by DerbysGal View Post
    It only gave me that error when I moved the columns around beforehand. It worked perfectly on the sample spreadsheet.
    I retested it after repositioning the Columns, and I still cannot duplicate the error?

  11. #11
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    It looks like I only get the error when I place one of the address columns in column A or B. Besides that it is working great.

  12. #12
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Move Data From One Column to Another based on column headers and Row Text

    OK. The way it was written, that would cause an error. Here is a corrected version:

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    YOU ARE AMAZING! THIS IS PERFECT!
    I wish I could something for you. Is there a place to rate you or sing your praises? Let me know and I will gladly do it.
    I hope you have a great day and thanks again!

  14. #14
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Move Data From One Column to Another based on column headers and Row Text

    You're welcome. Glad to help out and thanks for the feedback. If you wish to acknowledge just click the Add Reputation star at the bottom of my post.

  15. #15
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    JOHN H. DAVIS
    I have another problem. I did not take into account that there could be business names or sub-division names in “ADDRESS1” and/or “ADDRESS2” as well. They also did not tell me that there could be 4 address lines, not just three. This just keeps getting better.
    Is there a way to only move the PO Box to the previous address line number if the previous address line starts with a number?
    I have placed a couple more examples in the spreadsheet below for you to view. Hopefully that will clear up what I am trying to say.

    PO Box Address Test V2.xlsx

  16. #16
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Move Data From One Column to Another based on column headers and Row Text

    Try:

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    This works great, but sometimes ADDRESS4 will not be present. When I deleted it I got an error again. Is it possible for it to look IF ADDRESS4 is there?

  18. #18
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Move Data From One Column to Another based on column headers and Row Text

    Try:

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    Is there anything you cannot do?! You are great, thank you!

  20. #20
    Registered User
    Join Date
    07-26-2011
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Move Data From One Column to Another based on column headers and Row Text

    I think I rated you correctly, let me know if I did not. You deserve a great rating. I appreciate your help.

  21. #21
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Move Data From One Column to Another based on column headers and Row Text

    You're welcome. Glad to help out and thanks for the feedback and 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. [SOLVED] HELP! Trying to move data in rows to match column headers
    By ablits1 in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 08-31-2015, 07:00 PM
  2. Replies: 12
    Last Post: 01-08-2014, 11:28 AM
  3. Move data in column B based on value in column A see attached example
    By Liberator in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 02-16-2013, 03:55 AM
  4. Move data in column B based on value in column A see attached example
    By Liberator in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-15-2013, 02:02 AM
  5. Replies: 20
    Last Post: 05-15-2012, 04:42 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