+ Reply to Thread
Results 1 to 4 of 4

Adding Lines based on Text in a Cell

  1. #1
    Registered User
    Join Date
    01-31-2017
    Location
    St. Louis
    MS-Off Ver
    2013
    Posts
    5

    Adding Lines based on Text in a Cell

    Is there a way to create a macro to add a new line based off of a certain text in a cell?

    Per my screenshot below I want to add a new line every time the state changes. I am thinking we can key off of the first 3 characters in the cell. For example, the first line would be inserted just above the first FL cell. So if the macro can key off of "EFL" that will be very helpful. I do not want to include the numbers in the macro because the numbers behind the first 3 characters change weekly, but the states do not. Also, I cannot write a macro based on row number as new rows get added weekly. Which is why I want to base the addition of a new line on the characters in the cell.

    I highlighted in yellow where I want to add new lines. The second screenshot is how I want the final product to look like.

    This is a subset of my data, so creating this will save me a lot of time!

    Also, I am a beginner at writing macros, so any explanation of what you wrote will help in case I need to edit later or drop in with another macro. Thank you!


    Capture.JPG Capture2.JPG
    Attached Files Attached Files

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Adding Lines based on Text in a Cell

    Try this:
    Please Login or Register  to view this content.
    It loops through your data in Column A from the bottom up, and for each row, compares the first n characters with the cell above (as long as both cells have something in them). If they are different, then a row is inserted. The number of characters which defines a match is controlled with parameter iLeftChars.

    The Application stuff is to stop the screen flickering, and speed up performance, when applying to large datasets. The error handling makes sure that we reset the application stuff, even if an error happens, and that errors don't cause a horrible message to be displayed.

    Make sense?
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: Adding Lines based on Text in a Cell

    This code should do the trick.

    Please Login or Register  to view this content.
    Please do add reputation where you see fit, it's nice to be nice and we all enjoy a pat on the back

    Please also mark your thread as solved once it has been.

  4. #4
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: Adding Lines based on Text in a Cell

    Or Olly's!

+ 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. Insert Lines based on Cell value, Find Last Occurrence of text, Delete Previous Informatio
    By FranktheBank in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-11-2015, 03:49 PM
  2. Replies: 9
    Last Post: 07-24-2015, 12:40 PM
  3. [SOLVED] Adding text to a cell based on specific text characters in another cell
    By JohnnyBoyxxx in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-29-2014, 06:03 PM
  4. VBA for Adding A Standard Text On First Blank Lines
    By angelab in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-29-2014, 01:39 PM
  5. [SOLVED] Print lines to a text file based on change in cell contents
    By cthreepo986 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-28-2013, 09:53 AM
  6. Adding lines ABOVE based on number of commas used.
    By Lifeseeker in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 06-26-2012, 04:03 PM
  7. Adding multiple lines of text within one cell
    By Michael in forum Excel General
    Replies: 2
    Last Post: 04-20-2005, 02:06 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