+ Reply to Thread
Results 1 to 11 of 11

VBA Macro to loop text to columns through all rows of data

  1. #1
    Registered User
    Join Date
    04-17-2014
    Location
    CO
    MS-Off Ver
    Office 2010
    Posts
    5

    VBA Macro to loop text to columns through all rows of data

    Hi,

    I've been visiting this forum through Google search to help me with Excel questions for a while now, and it seems like the help is consistently timely and effective for other users' specific questions, so I went ahead and registered to post a question of my own.

    I am trying to get a macro to run in excel that takes a simple text to columns command in one line of data and runs the command on a loop through however many rows of data there happen to be. I thought getting this to work would be more straightforward, but I've been trying unsuccessfully for a while now, which led me to seek help from the experts on this board.

    I've attached two screenshots - one with what I've got now (Before.jpg) and what I'd like to have after the macro runs (After.jpg). The code below is what I used to get the first text to column breakout, which I can hopefully run on a loop to breakout anything in the DEPT column that contains a "/". It can ignore the rows that only have one department to begin with.

    Selection.TextToColumns Destination:=Range("K2"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
    :="/", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

    Eventually I'll want to create another macro that transposes the breakout text back into the DEPT column and populates it with the corresponding data, but I figure I'll take things one step at a time.

    I think I've provided enough info for anyone who wants to assist, but if not let me know what else I can do. Thanks in advance for the help!
    Attached Images Attached Images

  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
    49,452

    Re: VBA Macro to loop text to columns through all rows of data

    Why do you need to loop? Text to Columns works on a range, or column, you don't need to do it cell by cell.

    Record a macro while you select the whole of column E and use Text to Columns to output to column K.


    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
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA Macro to loop text to columns through all rows of data

    Does this help?

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    04-17-2014
    Location
    CO
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: VBA Macro to loop text to columns through all rows of data

    TMS - Thanks for the reply. While I'm aware that I can select the range and do text to columns, I guess I was looking to add an additional element of automation to what is going to become a larger macro. The main piece of what I need to do involves the transposition step I mentioned above - basically cutting out the data in those breakout columns and pasting them back into that original DEPT column, inserting rows and populating the other fields accordingly. I wanted to have it so that I could just produce my data list (of variable length), run the macro, and have the text to columns/transposition happen all at once.

    What you're saying makes sense though, and I guess there are other ways to go about the text to column piece. That being said, I'm going to need help with the transposition macro as well. Any ideas? I've attached another screenshot to show how I'd like the data to end up.

    Thanks.
    Attached Images Attached Images

  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
    49,452

    Re: VBA Macro to loop text to columns through all rows of data

    Any ideas ... post a sample workbook with data and code, and an explanation of what it is you need to achieve ... with pictures, if you must.

    Have you tried John's code? Does that do what you want?

    Regards, TMS

  6. #6
    Registered User
    Join Date
    04-17-2014
    Location
    CO
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: VBA Macro to loop text to columns through all rows of data

    John - This works perfect! Thanks for the code on that. If you have any input on the transposition piece I'm looking to do next, I'd love to hear from you.

  7. #7
    Registered User
    Join Date
    04-17-2014
    Location
    CO
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: VBA Macro to loop text to columns through all rows of data

    TMS - John's code works. Sample workbook coming soon. Sounds like pictures aren't the ideal? If not, just let me know. Please standby.

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

    Re: VBA Macro to loop text to columns through all rows of data

    Try:

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    04-17-2014
    Location
    CO
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: VBA Macro to loop text to columns through all rows of data

    John - That is exactly what I needed! I really appreciate the help! You saved me a lot of time and struggle. Cheers.

  10. #10
    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
    49,452

    Re: VBA Macro to loop text to columns through all rows of data

    Sounds like pictures aren't the ideal?
    No. If you provide a workbook, it is much easier to see what is required and also to test the possible solution(s).

    Not everyone will create a test harness so they can develop the code, especially, if there are a lot of sheets and/or data.

    But, seems John has provided you with what you need.

    Regards, TMS

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

    Re: VBA Macro to loop text to columns through all rows of data

    You're welcome. Glad to help out and thanks for the feedback. Please comply with Forum Rule No. 9.

    9. Acknowledge the responses you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click Thread Tools above your first post, select "Mark your thread as Solved". Or click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Dropdown option or Edit button will not appear -- ask a moderator to mark it.

+ 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 Across Columns...If Blank Cell, Loop Down Rows
    By flindy87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2014, 07:05 PM
  2. Loop Across Columns If Blank Cell Loop Down Rows
    By flindy87 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2014, 10:22 AM
  3. Macro to loop between multiple columns and copy the text in one column
    By Manish_Gupta in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-14-2013, 06:41 AM
  4. Macro loop for Worksheets with same # columns but different # rows
    By rcumeras in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-16-2010, 07:31 AM
  5. loop or macro to retrieve rows of data from other worksheet
    By beatrice25 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-17-2008, 12:11 PM

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