+ Reply to Thread
Results 1 to 10 of 10

VBA to delete a row if NOT exist...

  1. #1
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    VBA to delete a row if NOT exist...

    Hello VBA guru's - - I have a large excel file that column A contains zip codes. All the zip codes are in Illinois to be exact. But I only need to see about 30 zipcodes. I have some code below that will delete the row if the value does not exist. I'd like to create some sort of variable or Dim where I can list the 30 or so zip codes then reference this variable. I'm getting lost on the syntax so a little help would be great!

    I'm thinking something like an 'in' in sql? But I'm not 100% sure how that would be done. Is there a way to include another dim that would list each zip, like zipnumber = (60110,60030,60011) instead of repeating the .value as in my example below? Then my if would look more like if .value = zipnumber then delete...? make sense?

    Thanks for your thoughts/assistance! Here's my test code (which I got from this great forum!!!!

    Please Login or Register  to view this content.
    Last edited by Ironman; 10-07-2010 at 11:32 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to delete a row if NOT exist...

    Hello ironman,

    This macro uses "Sheet1" and "Sheet2". Sheet1 holds all the zip codes in column "A". Sheet2 column "A" holds the zip codes you want to see. You can change the worksheet names and columns if needed. The macro uses a Dictionary object to test if the desired zip code exists.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: VBA to delete a row if NOT exist...

    Hi Leith - - -

    I like your idea, I am getting a run time error '1004' at the line

    Please Login or Register  to view this content.
    Any suggestions?

    Thanks again!

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to delete a row if NOT exist...

    Hello Ironman,

    The Dictionary object was introduced in Office 2000. In the VB Editor go to to Tools > References... Check the refernces for any MISSSING: libraries. Let me know what you find.

  5. #5
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: VBA to delete a row if NOT exist...

    I've checked and there is nothing listed with MISSING:, I'm assuming that the word would be listed first. There are four items checked: Visaul Basic For Applications, Microsoft Excel 11.0 Object Lib, OLE automation and Microsoft Office 11.0 Object Lib.

    The rest remain unchecked.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to delete a row if NOT exist...

    Hello ironman,

    The references listed are normal. Can you post a copy of the workbook for review?

  7. #7
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: VBA to delete a row if NOT exist...

    Thanks for checking, I'm attaching a very basic test file....been awhile since I did this, not sure if it worked?! The file name is ziptest.xls. thanks again. I'm running Excel 2003.
    Attached Files Attached Files

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to delete a row if NOT exist...

    Hello Ironman,

    I made a few changes to the macro. One of which is to save the amount next to the zip code. I figured you will probably need that later on. I have added a button on the Zip worksheet to run the macro. This works on my system which is also a 2003 version. The attached workbook has all the changes made to it.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: VBA to delete a row if NOT exist...

    Thanks Leith - - That looks great! The actual file would contain a few more columns and 1000's of rows of data. I tested this on a file that contained roughly 5000 rows and it worked like a charm!

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA to delete a row if NOT exist...

    Hello Ironman,

    I am glad it is working well. If you need to make changes or have any problems, let me know.

+ 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