+ Reply to Thread
Results 1 to 17 of 17

Macro to delete row if the first characters are numbers

  1. #1
    Registered User
    Join Date
    05-20-2014
    Posts
    5

    Macro to delete row if the first characters are numbers

    Hi everyone. This is my first post.

    I am new to writing VBA macros. I am trying to find a way to remove rows that start with three numbers and then a space. I need the macro to perform this only for the selected row and afterwards jump to the next one (this is because sometimes I want to keep the row even if it starts with three numbers and a space)

    07373 Hollolan peruskoulun yläaste
    Terveystie 8 ja 10
    15870 Hollola
    138 Peruskoulun erityiskoulut
    07385 Kankaan koulu
    Opintie 3
    15870 Hollola
    162 Lukiot

    Thanks in advance!

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to delete row if the first characters are numbers

    Please Login or Register  to view this content.
    is what you'll need and to compare, if
    Please Login or Register  to view this content.
    will be the check. Youll need a for next loop which is startin at
    Please Login or Register  to view this content.
    and ending in
    Please Login or Register  to view this content.
    Its worth mentioning, its better to work backwards this way otherwise counts can get messed up, as if you delete a row and increment your counter, you will skip the one after you've deleted.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    05-20-2014
    Posts
    5

    Re: Macro to delete row if the first characters are numbers

    Thanks for your prompt response nathan! How do I fuse those bits into a working code? Yes, I'm that new to this .

  4. #4
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Macro to delete row if the first characters are numbers

    nathansav ,
    wouldn't this delete also numbers with more than 3 digits?

    because 3rd digit is number also at 99999

  5. #5
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro to delete row if the first characters are numbers

    Hi..

    Try this.. see attached file to show it working..

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

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to delete row if the first characters are numbers

    yes, you could do isnumeric(left(range("a1").value,3)) and mid(range("a1").value,3,1)=chr(32)

  7. #7
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro to delete row if the first characters are numbers

    @ nathansav

    I am pretty sure the OP wants to iterate through a column of strings and give him the option to delete the ones that have 3 digits followed by a space at the beginning..

    Then again.. I could be wrong..

  8. #8
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to delete row if the first characters are numbers

    "to perform this only for the selected row and afterwards jump to the next one (this is because sometimes I want to keep the row even if it starts with three numbers and a space)"


    i think its to start where he wants it

  9. #9
    Registered User
    Join Date
    05-20-2014
    Posts
    5

    Re: Macro to delete row if the first characters are numbers

    Thanks apo. Your code does exactly what I requested! Is it possible to make it work with a macro shortcut instead of a button? Also how can I get the button to show in my own worksheet? I tried copying your code to the module of VBA.

  10. #10
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro to delete row if the first characters are numbers

    Put the following in a Module.

    Note: Change the name of the sheet if needed..

    Please Login or Register  to view this content.
    Then Click "Macros" on the Developer Tab (Enable this if it isn't already)... Select the "Delete" macro and then Click the Options Button..

    You will then see a section where you can set a Shortcut to run the Macro..

  11. #11
    Registered User
    Join Date
    05-20-2014
    Posts
    5

    Re: Macro to delete row if the first characters are numbers

    Thanks apo!

  12. #12
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro to delete row if the first characters are numbers

    See my previous post..

    Then Click "Macros" on the Developer Tab (Enable this if it isn't already)... Select the "Delete" macro and then Click the Options Button..

    You will then see a section where you can set a Shortcut to run the Macro..

  13. #13
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro to delete row if the first characters are numbers

    lol.. that's twice in one thread that someone has edited their post and made me look like I am talking to myself..

    I am not going mad.. I swear..

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Macro to delete row if the first characters are numbers

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    05-20-2014
    Posts
    5

    Re: Macro to delete row if the first characters are numbers

    Apo: your code works like charm, except I would like it run in whichever sheet is active instead of Sheet1. Also, sorry for causing confusion with post editing.

  16. #16
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro to delete row if the first characters are numbers

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

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Macro to delete row if the first characters are numbers

    Quote Originally Posted by Ilwuen View Post
    I need the macro to perform this only for the selected row and afterwards jump to the next one (this is because sometimes I want to keep the row even if it starts with three numbers and a space)
    Please Login or Register  to view this content.
    Note:
    Deleting rows/columns with For Each loop will fail if you are deleting consecutive rows/columns.

  18. #18
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Macro to delete row if the first characters are numbers

    test test test

  19. #19
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro to delete row if the first characters are numbers

    Note:
    Deleting rows/columns with For Each loop will fail if you are deleting consecutive rows/columns.
    Thanks for pointing that out Jindon.

    @ Ilwuen.. If you want to use my code you will have to change the For Each loop to a For Next loop and step backwards (similar to what Jindon has done)..

    If stepping through the rows from bottom of list to top fees a bit 'un-natural'... you could use my existing code and instead of deleting each row as you go.. form a range to delete using Union method and delete them all in one go at the end..

    Like:
    Please Login or Register  to view this content.

+ 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. Macro to Delete all but first 3 characters
    By megdawom in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2011, 07:46 PM
  2. Macro to delete last 2 characters in a cell
    By rbichamp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2010, 01:36 PM
  3. How to delete all the characters in cell and leave just the numbers?
    By yannipr in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-11-2008, 11:23 AM
  4. Macro needed to delete characters
    By Dutchbeer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-23-2007, 05:24 AM
  5. macro to delete all characters in a row before @ character
    By chcal301 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2005, 04:49 AM

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