Solved

Excel VBA

Posted on 2014-02-23
7
361 Views
Last Modified: 2014-02-23
Hello,
can you please help,
I'm using below code to compare sheets Columns and then Delete rows.
right now, I run it one sheet at a time,
can I run this for multiple sheets.

Example
    Set ws1 = Sheets("COM116")
I have sheets
COM58, COM116, COM170,COM135, COM150, COM235 , ..... and more..

Sub TESTTTT()
Sub Compare_Delete()
    Dim i As Long
    Dim iLastRow As Long, xLastRow As Long
    Dim ws As Worksheet, ws1 As Worksheet
     
    Set ws = Sheets("COM1")
    Set ws1 = Sheets("COM116")

     'ws1.Visible = xlSheetVisible
    xLastRow = ws1.Range("A5000").End(xlUp).Row
    With ws
        iLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        For i = iLastRow To 2 Step -1
            For x = xLastRow To 2 Step -1
                If .Cells(i, "A").Value = ws1.Cells(x, "A").Value And _
                .Cells(i, "B").Value = ws1.Cells(x, "B").Value And _
                .Cells(i, "C").Value = ws1.Cells(x, "C").Value And _
                .Cells(i, "W").Value = ws1.Cells(x, "W").Value Then
                    .Rows(i).Delete
                End If
            Next x
        Next i
    End With
End Sub

Any help is appreciated.
thanks
0
Comment
Question by:Wass_QA
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:Norie
Comment Utility
What other sheets do you want to compare?
0
 

Author Comment

by:Wass_QA
Comment Utility
Hello,
COM212
COM216
COM222

thanks
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Do you want to compare them to COM1 or to each other?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Wass_QA
Comment Utility
to COM1
0
 
LVL 33

Accepted Solution

by:
Norie earned 300 total points
Comment Utility
Perhaps.
Sub Compare_Delete()
Dim i As Long
Dim iLastRow As Long, xLastRow As Long
Dim ws As Worksheet, ws1 As Worksheet

    Set ws = Sheets("COM1")
    For Each ws1 In ActiveWorkbook.Worksheets

        If ws1.Name Like "COM*" And Not ws1 Is ws Then


            xLastRow = ws1.Range("A5000").End(xlUp).Row
            With ws
                iLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
                For i = iLastRow To 2 Step -1
                    For x = xLastRow To 2 Step -1
                        If .Cells(i, "A").Value = ws1.Cells(x, "A").Value And _
                           .Cells(i, "B").Value = ws1.Cells(x, "B").Value And _
                           .Cells(i, "C").Value = ws1.Cells(x, "C").Value And _
                           .Cells(i, "W").Value = ws1.Cells(x, "W").Value Then
                            .Rows(i).Delete
                        End If
                    Next x
                Next i
            End With
        End If
    Next ws1
    
End Sub

Open in new window

0
 

Author Comment

by:Wass_QA
Comment Utility
Hello,
thank you , this is working,

But,
how do i exclude some sheets that start with COM ?

example,
COM123
COM321

thanks
0
 

Author Closing Comment

by:Wass_QA
Comment Utility
Got it,
Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Earnings Spreadsheet in Excel 3 36
Excel formula 6 20
Copying and pasting pictures from Excel 2 15
integer8 values 1 9
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now