+ Reply to Thread
Results 1 to 10 of 10

Basic Macro language: Remove spaces, filter on criteria

  1. #1
    Registered User
    Join Date
    07-25-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    88

    Basic Macro language: Remove spaces, filter on criteria

    Hello,

    As a VBA beginner, I'm still trying to get a feel for how to write macros.

    A common issue I have is the need to delete entire rows that contain either blanks or certain text within them, and then shift up to cover the deleted row. Another thing I'd like to learn how to do is how to insert a row below a given row if a certain cell contains specific text.

    I've attached an example workbook to illustrate what I'd like to do. Column A contains company number, B contains the type of company, C contains company name, and D contains a value for the company. How can I create a macro to:

    1). Delete rows where no name is provided and shift up
    2). Insert a row between two rows if the "Type" field is different - (e.g. insert a row between row 4 and 5, or 16 and 17, which have different value for Type).

    Thanks to all for the help, and for taking the time to read my question.

    -Student1990



    FilterRows_Example.xlsx

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

    Re: Basic Macro language: Remove spaces, filter on criteria

    ONLY the first one-No loop, but with second one, you need to loop through the rows.

    Please Login or Register  to view this content.

  3. #3
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Basic Macro language: Remove spaces, filter on criteria

    maybe
    Please Login or Register  to view this content.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Basic Macro language: Remove spaces, filter on criteria

    If deleting rows has to be a loop (if you testing for a particular string) then maybe:
    Please Login or Register  to view this content.
    You can put any literal value between "" or use InStr() to test part of the string.

  5. #5
    Registered User
    Join Date
    07-25-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Basic Macro language: Remove spaces, filter on criteria

    Thank you AB33. So .SpecialCells(4) - does that mean a blank cell?

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

    Re: Basic Macro language: Remove spaces, filter on criteria

    Yes, it is

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-25-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Basic Macro language: Remove spaces, filter on criteria

    Thanks all. Between these two solutions, I'm starting to see how to filter on certain criteria. Thank you both!

  8. #8
    Registered User
    Join Date
    07-25-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Basic Macro language: Remove spaces, filter on criteria

    Sample2_InsertRows.xlsxI'm curious now how to insert rows based on the presence of certain text, other than just a blank space. In the attached workbook, I want to know how to remove rows with a space in column N - then as a second step, insert a row if a value in column C differs from the value in the row immediately below it. I can't figure out the proper way to loop through and check each row in column C. Can you guys help me out? Thanks again.

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

    Re: Basic Macro language: Remove spaces, filter on criteria

    Try the attached.
    To speed up the execution of the code, I first deleted the empty cells and then loop through the rows to insert rows.
    If you do not have empty cells, you may get an error on the first part of the code.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    07-25-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Basic Macro language: Remove spaces, filter on criteria

    That did the trick - thank you again, AB33.

+ 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] Macro to remove extra spaces in an Excel File
    By Ardiko in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-15-2013, 10:33 AM
  2. remove spaces and characters in hyperlink - macro
    By missy22 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2013, 09:23 AM
  3. Visual Basic Language Support
    By nashc0 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-25-2007, 05:03 AM
  4. Very basic (not the language) Macro help
    By phattony72 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-24-2006, 09:19 PM
  5. [SOLVED] macro to remove spaces at the end of a cell
    By Michael A in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-12-2005, 08:06 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