+ Reply to Thread
Results 1 to 26 of 26

How to insert row above specific text

  1. #1
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    How to insert row above specific text

    Hello I am trying to craft a macro in Excel 2010 that will search my 1 sheet worksheet and insert a row above the text I specify.

    For example I have a field mobile:***-***-****, I found code that will insert rows every time "mobile" appears, but it won't work for me because mobile will always have different numbers attached to it. So in a sense I need a code that will look for mobile + wildcards and then insert a row above it that will say "replace: mobile"

    Here is the current code I found from a previous thread on here:
    Please Login or Register  to view this content.
    This works great to create a row BELOW "mobile", but I need it to make a row above mobile + wildcards.

    Any help would be greatly appreciated!!

  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: How to insert row above specific text

    Maybe:

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    Quote Originally Posted by JOHN H. DAVIS View Post
    Maybe:

    Please Login or Register  to view this content.
    That worked for finding *mobile* and then creating a blank row above it, but it never stopped creating rows.

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    Generally speaking, avoid using Do/While and instead use for next. It is also accurate to insert from bottom up.

    Please Login or Register  to view this content.

  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: How to insert row above specific text

    Try:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    Quote Originally Posted by AB33 View Post
    Generally speaking, avoid using Do/While and instead use for next. It is also accurate to insert from bottom up.

    Please Login or Register  to view this content.
    This created a blank row 2 spaces above where "mobile" appeared. Your first code:
    Please Login or Register  to view this content.
    worked great, if I could just get it to create a line above "mobile" and be able to specify what that line says without it looping forever.

  8. #8
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    Please Login or Register  to view this content.
    This works, but it creates the row below "mobile", if I can just get it to create it above mobile without breaking id be all set.

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    Change this line
    Please Login or Register  to view this content.
    INTO
    Please Login or Register  to view this content.

  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: How to insert row above specific text

    Quote Originally Posted by KazigluBey View Post
    Please Login or Register  to view this content.
    This works, but it creates the row below "mobile", if I can just get it to create it above mobile without breaking id be all set.
    As stated earlier by AB33 it is better code from the bottom up, but if you are hooked on the code you originally provided
    just change the plus's to minus's

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

  11. #11
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    Quote Originally Posted by JOHN H. DAVIS View Post
    As stated earlier by AB33 it is better code from the bottom up, but if you are hooked on the code you originally provided
    just change the plus's to minus's

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I am not impartial to any code, just this one seemed easiest to fix. I tried the - instead of + and it just loops until crash.

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    Please attach a sample so that we can easily see what is going.

  13. #13
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    Quote Originally Posted by AB33 View Post
    Please attach a sample so that we can easily see what is going.
    Ok with this code:
    Please Login or Register  to view this content.
    I get this: first macro.JPG

    With this code:
    Please Login or Register  to view this content.
    I get this: macro test 2.JPG

    If I replace + with - it puts row above "mobile" but loops till crash.

  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: How to insert row above specific text

    Can you attach a sample xl file with data so we can test?

    To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.

  15. #15
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    Macro test.xlsx
    Quote Originally Posted by JOHN H. DAVIS View Post
    Can you attach a sample xl file with data so we can test?

    To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.
    Yes of course.

  16. #16
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    Quote Originally Posted by AB33 View Post
    Please Login or Register  to view this content.
    This works great so far, any idea what the syntax would be to have it type "replace: mobile" in the blank line it creates?

  18. #18
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    Kazi,
    Please do not reply with quote. Just a reply will do.

    Which row are you referring on the attached?

  19. #19
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    Edit*
    Bingo that new code worked like a charm.
    Thank you so much!

    Just 1 more request, is there anyway to code this to also create a blank line UNDER "mobile*" and have it input - in it?
    Last edited by KazigluBey; 07-12-2013 at 11:41 AM.

  21. #21
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    I can use both of your codes as separate macros and get it do exactly what I want. I won't trouble you anymore with combining them. Thank you so much for all of your help!

  23. #23
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    Try the attached.
    Attached Files Attached Files

  24. #24
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    That'll do it. Thank you!

  25. #25
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: How to insert row above specific text

    You should also thank you John for his equally contribution to the thread.
    Could you please now close (Mark) this thread as solved? Go to the top right-hand side of this page, choose "Thread Tools" from the menu, then select "solved" from the drop down menu.

  26. #26
    Registered User
    Join Date
    07-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: How to insert row above specific text

    Yes of course. I submitted reputation to both of you for saving me hours and hours of work! Thank you both!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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