+ Reply to Thread
Results 1 to 18 of 18

Compare 2 Worksheets based on single criteria and then delete row

  1. #1
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Compare 2 Worksheets based on single criteria and then delete row

    Sheet1 in test.xlsm includes 2 columns listing different fruits (column A) and type (Column B). I would like to delete rows that do not equal "Mango". The "Mango" criteria is listed in Sheet1 in Criteria.xlsm. Problem: everything is deleted but the 2nd row that has fruit "Grape". What should happen is that only rows that don't have the fruit "Mango" should be deleted.

    Here is my code and it is executed in Sheet1 in test.xlsm (Both files (test and Criteria) attached. Thank you so much!

    Sub DeleteDifferentWorkbook()

    Application.ScreenUpdating = False
    Dim iRow As Integer
    Dim lr As Long
    Dim List As Variant
    Dim y As Workbook

    lr = ThisWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Set y = Workbooks.Open("C:\Users\jojoshu\Desktop\VBA Test\Criteria.xlsm")
    List = y.Worksheets("Sheet1").Cells(2, "A").Value

    For iRow = lr To 1 Step -1
    If Cells(iRow, "A").Value <> List Then
    ThisWorkbook.Worksheets("Sheet1").Rows(iRow).Delete
    End If
    Next iRow

    Application.ScreenUpdating = True

    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,819

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Try:
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Getting error message
    Error Message.JPG
    Code with Error Line Highlighted.JPG

    I would prefer to keep it simple by using the simple loop and "if and End if" methods instead of autofilter method

    The following code works beautifully when the rows to delete data (Sheet1) and the Criteria (Sheet2) worksheets are in the same workbook:

    Sub Delete()

    Application.ScreenUpdating = False
    Dim i As Integer
    Dim lr As Long
    Dim List As String

    lr = Range("A" & Rows.Count).End(xlUp).Row
    List = Worksheets("Sheet2").Cells(2, "A").Value

    For i = lr To 1 Step -1
    If Cells(i, "A").Value <> List Then
    Worksheets("Sheet1").Rows(i).Delete
    End If
    Next i

    Application.ScreenUpdating = True

    End Sub

  4. #4
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Getting error message
    Attachment 606366
    Attachment 606367

    I would prefer to keep it simple by using the simple loop and "if and End if" methods instead of autofilter method

    The following code works beautifully when the rows to delete data (Sheet1) and the Criteria (Sheet2) worksheets are in the same workbook:

    Sub Delete()

    Application.ScreenUpdating = False
    Dim i As Integer
    Dim lr As Long
    Dim List As String

    lr = Range("A" & Rows.Count).End(xlUp).Row
    List = Worksheets("Sheet2").Cells(2, "A").Value

    For i = lr To 1 Step -1
    If Cells(i, "A").Value <> List Then
    Worksheets("Sheet1").Rows(i).Delete
    End If
    Next i

    Application.ScreenUpdating = True

    End Sub

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,819

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Give this version a try:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Thank you Mumps1. After making a slight change to your code, it works perfectly. I would very much appreciate it if you could at your spare time (no rush) as to why my original code with the loop and if then statement did not work. As I stated to you earlier when executing code within same workbook the code works fine. What is it about executing the code with 2 workbooks that it does not work?

    Once again thank you so much for your prompt response and assistance.

  7. #7
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Thank you Mumps1. After making a slight change to your code, it works perfectly. I would very much appreciate it if you could at your spare time (no rush) look into as to why my original code with the loop and if then statement did not work. As I stated to you earlier when executing code within same workbook the code works fine. What is it about executing the code with 2 workbooks that it does not work?

    Once again thank you so much for your prompt response and assistance.

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,819

    Re: Compare 2 Worksheets based on single criteria and then delete row

    You are very welcome. When you open a workbook, that workbook becomes the active workbook so any code that follows the "Open" line of code applies to the newly opened workbook. If you want to have the code apply to the "test.xlsm" workbook, then you have to fully qualify all the references to that workbook so this:
    Please Login or Register  to view this content.
    becomes this:
    Please Login or Register  to view this content.
    Note the dot notation (in red) which makes those lines refer to Sheet1 in "test.xlsm. I hope this helps.

  9. #9
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Thanks for introducing me Autofilter methodolgy and also, that when working with multiple workbooks, statements have to be fully qualified. Not only did you do a beautiful job of providing the solutions but also the explanations to the problem/solution.

    Awesome!!!!

  10. #10
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,819

    Re: Compare 2 Worksheets based on single criteria and then delete row

    You are very welcome. Looping can be very slow compared to filtering so if you have a large data set, it will make a difference.

  11. #11
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Compare 2 Worksheets based on single criteria and then delete row

    What if my List referenced 10+ rows, would Autofilter work or would the loop method be more suitable for this task?

    I made the following change but got type mismatch error (Runtime error 13):

    List = y.Worksheets("Sheet1").Cells(2, "A").Value
    List = y.Worksheets("Sheet1").Range("A2:A11").Value



    Sub DelDifferentWkbk_Loop()

    Application.ScreenUpdating = False
    Dim iRow As Integer
    Dim lr As Long
    Dim List As String
    Dim y As Workbook

    lr = ThisWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Set y = Workbooks.Open("C:\Users\jojoshu\Desktop\STARS VBA Test\Criteria.xlsm")
    List = y.Worksheets("Sheet1").Range("A2:A11").Value

    With Workbooks("test.xlsm").Sheets("Sheet1")
    For iRow = lr To 1 Step -1
    If .Cells(iRow, "A").Value <> List Then
    .Rows(iRow).Delete
    End If
    Next iRow
    End With

    Application.ScreenUpdating = True

    End Sub

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,819

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Are you saying that you want to delete rows based on more than one value?

  13. #13
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Yes and those values have to reference a range of cells, say A1 to A11.

    Thanks!

  14. #14
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,819

    Re: Compare 2 Worksheets based on single criteria and then delete row

    That's a little trickier. Can you post a copy of your file and explain in detail what you want to do? De-sensitize the data if necessary.

  15. #15
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Sheet1 in test.xlsm includes 2 columns listing different fruits (column A) and type (Column B). I would like to delete rows in Sheet1 of test.xlsm file that do not equal the values listed in Sheet1 of Criteria.xlsm file.
    Attached Files Attached Files

  16. #16
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,819

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Try:
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    04-11-2014
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Compare 2 Worksheets based on single criteria and then delete row

    After 2 minor adjustments, your code works fine. Thanks. Now I have to study it as you have introduced new terminology that I am unfamiliar with.

  18. #18
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,819

    Re: Compare 2 Worksheets based on single criteria and then delete row

    Here is the macro with some explanatory comments:
    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. Compare two excel workbooks (or two worksheets within a single workbook)
    By sergiozygmunt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2016, 03:11 AM
  2. [SOLVED] Delete Worksheets Based on Name from InputBox Criteria
    By d247 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-10-2014, 01:45 AM
  3. Replies: 1
    Last Post: 11-09-2014, 06:04 PM
  4. [SOLVED] Copy column data from multiple worksheets to single worksheet based on header criteria
    By nateaskins in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 03-21-2014, 10:42 AM
  5. Delete multiple rows of data based on criteria in a single cell
    By slaga9 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-12-2011, 03:41 AM
  6. Compare worksheets and delete those that don't match
    By Hootie in forum Excel General
    Replies: 3
    Last Post: 07-21-2010, 02:09 PM
  7. compare two worksheets and delete rows
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-26-2006, 07:45 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