+ Reply to Thread
Results 1 to 6 of 6

VBA - delete rows when two criterias match, otherwise do nothing

  1. #1
    Registered User
    Join Date
    03-08-2019
    Location
    Estonia
    MS-Off Ver
    MS Office 360
    Posts
    19

    VBA - delete rows when two criterias match, otherwise do nothing

    Hello,

    I am struggling with a VBA that should delete rows when two criterias match and if not, then keep everything as it was before.

    The thing is, that I have two worksheets: "Sheet1" and "Ladu"

    If value (product number) in "Sheet1" N16:N match with value in "Ladu" B4:B then values in the same row but in different column Sheet1(I:I) and Ladu(I:I) should be compared.

    If value in Ladu(I:I) is bigger than value in Sheet1(I:I) then delete entire row from Sheet1 (row where the matching value was found).

    If value in Ladu(I:I) is smaller than value in Sheet1(I:I) then do nothing.

    This is the code I've written, I hope you guys can give me some suggestions or tips.

    Please Login or Register  to view this content.
    Thank you in advance!

    Sample file:
    Attached Files Attached Files
    Last edited by VBwArrior; 04-05-2019 at 02:31 AM.

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: VBA - delete rows when two criterias match, otherwise do nothing

    Very sloppy code and incorrect, below the corrected for next loops

    code]
    Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False

    Dim Rng1 As Range
    Dim CompareRange As Range
    Dim Rng2 As Range
    Dim CompareRange2 As Range
    Dim x As Variant
    Dim y As Variant

    Set CompareRange = Worksheets("Ladu").Range("B4:B")
    Set Rng1 = Worksheets("Sheet1").Range("N16:N")

    Rng1.SpecialCells(xlCellTypeConstants).Select
    CompareRange.SpecialCells(xlCellTypeConstants).Select

    For Each x In Rng1
    For Each y In CompareRange
    If x = y And Worksheets("Sheet1").cell(x, 9).Value < Worksheets("Ladu").cell(y, 9).Value Then
    Worksheets("Sheet1").cell(x, 9).Value.EntireRow.Delete
    Else
    End If
    Next y
    Next x
    Application.ScreenUpdating = True
    End Sub
    [/code]

    But then ...

    Please Login or Register  to view this content.
    apart from that the two red marked lines, the first, that sheet is not present (as far as I can see)
    What is the lat row in N?

    The RefreshallDataConn in Laoseis is wrong, I'm sure you didn't even test it.

    I didn't even take the time to look further
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    03-08-2019
    Location
    Estonia
    MS-Off Ver
    MS Office 360
    Posts
    19

    Re: VBA - delete rows when two criterias match, otherwise do nothing

    Hi Keebellaah,

    I just made the sheet name easier to work with, so renamed it to "Ladu" in the code I have posted here.

    Did you mean the last row? If yes, then I would like to mark the range as full column N and B respectively.

    Thanks for your time and response!

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: VBA - delete rows when two criterias match, otherwise do nothing

    You could have least renamed the sheet yourself. Which sheet is / was it?

  5. #5
    Registered User
    Join Date
    03-08-2019
    Location
    Estonia
    MS-Off Ver
    MS Office 360
    Posts
    19

    Re: VBA - delete rows when two criterias match, otherwise do nothing

    Sheet named Laoseis was renamed to Ladu

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: VBA - delete rows when two criterias match, otherwise do nothing

    The code you used is incorrect but I don't know if the result is goor,

    here's the correct working code, it works because it contains no errors, it's your test with your data to verify.

    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. [SOLVED] Macro Find set criterias to a colomn of number, delete the rows that don't meet criterias
    By TAMMY32 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-03-2017, 04:09 PM
  2. Counting only if adjacent rows match with multiple criterias
    By jparve3283 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 03-22-2016, 05:23 PM
  3. Excel VBA to Delete rows based on multiple criterias
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 04-29-2015, 08:41 PM
  4. delete rows based on three criterias
    By cbk40060 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-15-2015, 02:26 PM
  5. [SOLVED] Delete rows based on multiple criterias, copy/paste data and formulas
    By BQuek in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-24-2013, 01:36 PM
  6. Singel out rows that match multiple criterias
    By Ewox in forum Excel General
    Replies: 2
    Last Post: 11-30-2010, 10:39 AM
  7. Delete rows based on multiple criterias
    By Benson in forum Excel General
    Replies: 8
    Last Post: 11-02-2005, 06:17 PM

Tags for this Thread

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