delete the data if condition met

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

Re: delete the data if condition met

Post by c@1234 »

i changed the code as i mentioned in the thread and i changed the path as u mentioned sir but that is not working
and this code is doing the process and file is opened and i want the process should be completed and save the file and close the file

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

Re: delete the data if condition met

Post by HansV »

c@1234 wrote:i changed the code as i mentioned in the thread and i changed the path as u mentioned sir but that is not working
Do you mean that the workbook isn't opened?
c@1234 wrote:and this code is doing the process and file is opened and i want the process should be completed and save the file and close the file
Add the following line above End Sub:

Code: Select all

    wsh.Parent.Close SaveChanges:=True
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 »

the workbook is opened but it has not deleted the row as per the condition

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

Re: delete the data if condition met

Post by HansV »

The sample workbook that you attached earlier in this thread didn't have any rows to be deleted.
Please attach a sample workbook that contains both rows to be kept and rows to be deleted.
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 the file
You do not have the required permissions to view the files attached to this post.

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

Re: delete the data if condition met

Post by HansV »

Do you want to keep a row if at least two of the three values are different, or if all three values are different?
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 »

if all three values are different from each other then keep that row and if not then delete

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

Re: delete the data if condition met

Post by HansV »

Does this version do what you want?

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.Open(ThisWorkbook.Path & "\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 Or _
                wsh.Range("D" & r).Value = wsh.Range("F" & r).Value Or _
                wsh.Range("E" & 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
    wsh.Parent.Close SaveChanges:=True
End Sub
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 this code works awesome thnx sir have a great day