delete the data if condition met

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

delete the data if condition met

Post by c@1234 »

my file name is sample.xlsm
vba will be in vba.xlsm file
if column D, column E and column F of sample.xlsm file are different (not same) then keep that row and if not then delete that complete row

User avatar
HansV
Administrator
Posts: 78574
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: delete the data if condition met

Post by HansV »

Welcome to Eileen's Lounge (or are you roger@/umpsbug/leonardo1234 ?)

See delete the rows based on conditions
Best wishes,
Hans

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

I am getting an error sir the problem is near to same but not same i tried but something went wrong

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

or are you roger@/umpsbug/leonardo1234 ?
No sir

User avatar
HansV
Administrator
Posts: 78574
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: delete the data if condition met

Post by HansV »

Please post the code you're using, and indicate which line causes the problem.
Best wishes,
Hans

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

i simply changed the column E,F,G to D,E,F but the code was not working

User avatar
HansV
Administrator
Posts: 78574
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: delete the data if condition met

Post by HansV »

Please post a sample workbook (without sensitive information)
Best wishes,
Hans

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

plz see
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78574
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: delete the data if condition met

Post by HansV »

There is no macro in the workbook...
Best wishes,
Hans

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

yes bcoz it was not working i simply changed that column in the code but it was not working

User avatar
HansV
Administrator
Posts: 78574
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: delete the data if condition met

Post by HansV »

Please post a workbook including the modified macro.
Best wishes,
Hans

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

Code: Select all

Sub DeleteRows()
    Dim wsh As Worksheet
    Dim r As Long
    Dim m As Long
    Dim rng As Range
    ' Refer to first sheet in Student.xlsx
    Set wsh = Workbooks("Students.xlsx").Worksheets(1)
    ' Find last used row in columns E to G
    m = wsh.Range("E:G").Find(What:="*", SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious).Row
    ' Loop through the rows, starting in row 2
    ' If you want to start in row 1, change 2 to 1
    For r = 2 To m
        ' Check whether columns E, F and G have the same value
        If wsh.Range("E" & r).Value = wsh.Range("F" & r).Value And _
                wsh.Range("E" & r).Value = wsh.Range("G" & r).Value Then
            ' Add cell in column A to the range to be deleted
            If rng Is Nothing Then
                Set rng = wsh.Range("A" & r)
            Else
                Set rng = Union(rng, wsh.Range("A" & r))
            End If
        End If
    Next r
    ' Do we have anything to delete?
    If Not rng Is Nothing Then
        ' If so, delete the entire row(s)
        rng.EntireRow.Delete
    End If
End Sub
instead of E,F,G
I putted D,E, F

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

Code: Select all

Sub DeleteRows()
    Dim wsh As Worksheet
    Dim r As Long
    Dim m As Long
    Dim rng As Range
    ' Refer to first sheet in Student.xlsx
    Set wsh = Workbooks("Students.xlsx").Worksheets(1)
    ' Find last used row in columns E to G
    m = wsh.Range("D:F").Find(What:="*", SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious).Row
    ' Loop through the rows, starting in row 2
    ' If you want to start in row 1, change 2 to 1
    For r = 2 To m
        ' Check whether columns E, F and G have the same value
        If wsh.Range("D" & r).Value = wsh.Range("E" & r).Value And _
                wsh.Range("D" & r).Value = wsh.Range("F" & r).Value Then
            ' Add cell in column A to the range to be deleted
            If rng Is Nothing Then
                Set rng = wsh.Range("A" & r)
            Else
                Set rng = Union(rng, wsh.Range("A" & r))
            End If
        End If
    Next r
    ' Do we have anything to delete?
    If Not rng Is Nothing Then
        ' If so, delete the entire row(s)
        rng.EntireRow.Delete
    End If
End Sub

User avatar
HansV
Administrator
Posts: 78574
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: delete the data if condition met

Post by HansV »

You have to change the name of the workbook in the code from Students.xlsx to Sample.xlsm
Best wishes,
Hans

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

ya i done that also but this code is not working

User avatar
HansV
Administrator
Posts: 78574
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: delete the data if condition met

Post by HansV »

What exactly is the problem? Just saying "this code is not working" doesn't tell me much.
Best wishes,
Hans

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

my file is not opened
as per the code file should be opened then only code will work

User avatar
HansV
Administrator
Posts: 78574
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: delete the data if condition met

Post by HansV »

Change the line

Code: Select all

    Set wsh = Workbooks("Sample.xlsm").Worksheets(1)
to

Code: Select all

    Set wsh = Workbooks.Open(ThisWorkbook.Path & "\Sample.xlsm").Worksheets(1)
Best wishes,
Hans

c@1234
Lounger
Posts: 25
Joined: 30 May 2019, 18:59

Re: delete the data if condition met

Post by c@1234 »

Sir after changing the same, the code is not working plz recheck

User avatar
HansV
Administrator
Posts: 78574
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: delete the data if condition met

Post by HansV »

What is the problem this time? Just mentioning "the code is not working" is not specific enough.
Best wishes,
Hans