+ Reply to Thread
Results 1 to 8 of 8

Macro for Comparison

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2014
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    83

    Macro for Comparison

    I have a macro that compares the datas in column A and B of the sheets 'Patches' and 'NA' and deletes the data in the 'Patch' sheet that matches with the ones in the 'NA' sheet.

    It works perfect except for one issue. There is a patch ,

    'Must Upgrade Product SP Before Applying Patch' in the 'Patches' sheet for a lot of machines whereas the same patch is present for only on machine (D) in the 'NA' sheet (Marked in Red).

    when i run the macro all the 'Must Upgrade Product SP Before Applying Patch' is deleted in the 'patches' sheet whereas it must be deleted only for the machine D.

    Sub NA()
    
     Const lngStartRow As Long = 2 'Starting data row number. Change to suit.
    Dim lngMyCol As Long, _
            lngMyRow As Long
        Dim xlnCalcMethod As XlCalculation
                
        With Application
            xlnCalcMethod = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        lngMyCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
        lngMyRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        With Columns(lngMyCol)
            With Range(Cells(lngStartRow, lngMyCol), Cells(lngMyRow, lngMyCol))
                .Formula = "=IF(ISERROR(VLOOKUP(B" & lngStartRow & ",NA!B:B,1,FALSE)),"""",NA())"
                ActiveSheet.Calculate
                .Value = .Value
            End With
            On Error Resume Next 'Turn error reporting off - OK to ignore 'No cells found' message
                .SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
            On Error GoTo 0 'Turn error reporting back on
            .Delete
        End With
        
        With Application
            .Calculation = xlnCalcMethod
            .ScreenUpdating = True
        End With
    
        MsgBox "NA patches have been deleted.", vbInformation
    
    End Sub
    can any one please help me on this? Thanks in advance!!
    Attached Files Attached Files
    Last edited by sarajun_88; 01-12-2015 at 05:46 AM. Reason: Code

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Macro for Comparison



    Must respect forum rules : code tags ‼

  3. #3
    Registered User
    Join Date
    11-22-2014
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    83

    Re: Macro for Comparison

    Sorry!! Edited as per the rule

  4. #4
    Registered User
    Join Date
    09-14-2004
    Location
    Jämtland, Sweden
    Posts
    16

    Re: Macro for Comparison

    How about this instead?

    Sub NA()
    Const StartRow_patches As Integer = 2
    Const StartRow_NA As Integer = 2
    Const WS_patches As String = "patches"
    Const WS_NA As String = "NA"
    
    Dim i As Integer
    Dim j As Integer
    Dim LastRow_patches As Integer
    Dim LastRow_NA As Integer
    
    Sheets(WS_patches).Select
    LastRow_patches = Sheets(WS_patches).Range("A25000").End(xlUp).Row
    LastRow_NA = Sheets(WS_NA).Range("A25000").End(xlUp).Row
    
    For i = StartRow_NA To LastRow_NA
        For j = LastRow_patches To StartRow_NA Step -1
            If (Sheets(WS_NA).Cells(i, 1) = Sheets(WS_patches).Cells(j, 1)) And (Sheets(WS_patches).Cells(i, 2) = Sheets(WS_patches).Cells(j, 2)) Then
                Sheets(WS_patches).Rows(j).Select
                Sheets(WS_patches).Rows(j).Delete
            End If
        Next j
    Next i
    
    End Sub

  5. #5
    Registered User
    Join Date
    11-22-2014
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    83

    Re: Macro for Comparison

    Sorry!! this didn't work the way I wanted

    Thank You!

  6. #6
    Registered User
    Join Date
    09-14-2004
    Location
    Jämtland, Sweden
    Posts
    16

    Re: Macro for Comparison

    It would help me to help you if you explained what the problem is...

  7. #7
    Registered User
    Join Date
    11-22-2014
    Location
    US
    MS-Off Ver
    Office 365
    Posts
    83

    Re: Macro for Comparison

    The macro needs to compare the datas in column A and B of the sheets 'Patches' and 'NA' and deletes the data in the 'Patch' sheet that matches with the ones in the 'NA' sheet.
    It should loop with data in the 'patches' sheet with the ones in the 'NA' sheet.
    There are multiple patches (column B) for the same server (Column A). so i want to check whether a server and its patches list in NA sheet matches if so it has to be deleted in the patches sheet.
    I have saved the result data which i want in the 'Result' for your reference.

    Option Explicit
    Sub Macro1()
        
        Const lngStartRow As Long = 2 'Starting data row number. Change to suit.
        
        Dim lngMyCol As Long, _
            lngMyRow As Long
        Dim xlnCalcMethod As XlCalculation
                
        With Application
            xlnCalcMethod = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        lngMyCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
        lngMyRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        With Columns(lngMyCol)
            With Range(Cells(lngStartRow, lngMyCol), Cells(lngMyRow, lngMyCol))
                .Formula = "=IF(ISERROR(VLOOKUP(B" & lngStartRow & ",NA!B:B,1,FALSE)),"""",NA())"
                ActiveSheet.Calculate
                .Value = .Value
            End With
            On Error Resume Next 'Turn error reporting off - OK to ignore 'No cells found' message
                .SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
            On Error GoTo 0 'Turn error reporting back on
            .Delete
        End With
        
        With Application
            .Calculation = xlnCalcMethod
            .ScreenUpdating = True
        End With
    
        MsgBox "Row(s) have now been deleted.", vbInformation
    
    End Sub
    this code works but it doesn't work for the patch ''Must Upgrade Product SP Before Applying Patch' alone. I have marked the patch in red.

  8. #8
    Registered User
    Join Date
    09-14-2004
    Location
    Jämtland, Sweden
    Posts
    16

    Re: Macro for Comparison

    1) I'm not sure that I'm following - columns A and B should match? But you only check column B?

    2) I also made an ugly mistake! Instead of
    If (Sheets(WS_NA).Cells(i, 1) = Sheets(WS_patches).Cells(j, 1)) And (Sheets(WS_NA).Cells(i, 2) = Sheets(WS_patches).Cells(j, 2)) Then
    I had
    If (Sheets(WS_NA).Cells(i, 1) = Sheets(WS_patches).Cells(j, 1)) And (Sheets(WS_patches).Cells(i, 2) = Sheets(WS_patches).Cells(j, 2)) Then

+ 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. comparison macro
    By beatrice25 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-29-2008, 03:14 PM
  2. Comparison Macro
    By seanmartin26 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2006, 05:57 PM
  3. RE: Comparison Macro
    By Tom Ogilvy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-26-2006, 01:20 PM
  4. Comparison Macro
    By aceensor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-20-2005, 03:13 PM
  5. [SOLVED] Macro for a comparison
    By mike b in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2005, 03:06 AM

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