Results 1 to 8 of 8

macro is showing as data missing but it is not missing and cant get it to amend.

Threaded View

  1. #1
    Forum Contributor
    Join Date
    08-02-2005
    Location
    LONDON
    MS-Off Ver
    OFFICE365
    Posts
    168

    macro is showing as data missing but it is not missing and cant get it to amend.

    Hi

    First of all I will admit that I have cheated a bit and copied from other macros into this one and this is my very first attempt at writing one. What I am trying to do is get it to look on sheet 3 and in column E there will be some entries that say "MISSING FROM TABLEAU" .

    Ive checked and the entries are not missing.

    What im trying to do is look on sheet 3 for the ones that are marked up "MISSING FROM TABLEAU" and compare against sheet called "TAB" . If the data is an exact match then I need that row deleting from sheet 3. When I mean an exact match I mean that data in columns A-D on the row must match.

    The data is not in line order on both sheets and could be anywhere in the sheet so I need it to look down all the rows for each individual entry.

    Can anyone help.

    Thanks

    Option Explicit
    
    Sub DeleteIdenticalRecordsFromTwoSheets()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim lr1 As Long, lr2 As Long, i As Long
    Dim x, y, xx(), yy(), dict1, dict2
    Dim delRng1 As Range, delRng2 As Range
    
    Application.ScreenUpdating = False
    
    Set ws1 = Worksheets("TAB")
    Set ws2 = Worksheets("Sheet3")
    
    lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row
    
    ws1.Range("A1:D" & lr1).RemoveDuplicates Columns:=Array(1, 2, 3, 4), Header:=xlNo
    
    lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    x = ws1.Range("A1:D" & lr1).Value
    y = ws2.Range("A1:D" & lr2).Value
    
    Set dict1 = CreateObject("Scripting.Dictionary")
    Set dict2 = CreateObject("Scripting.Dictionary")
    
    For i = 1 To UBound(x, 1)
        dict1.Item(x(i, 1) & x(i, 2) & x(i, 3) & x(i, 4)) = ws1.Range("A" & i).Address
    Next i
    
    For i = 1 To UBound(y, 1)
        dict2.Item(y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4)) = ws2.Range("A" & i).Address
    Next i
    
    ws1.Columns("E").Clear
    ws2.Columns("E").Clear
    
    
    
    For i = 1 To UBound(y, 1)
        If dict1.exists(y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4)) Then
            If delRng2 Is Nothing Then
                Set delRng2 = ws2.Range(dict2.Item(y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4)))
            Else
                Set delRng2 = Union(delRng2, ws2.Range(dict2.Item(y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4))))
            End If
        End If
    Next i
    
    
    If Not delRng1 Is Nothing Then delRng1.EntireRow.Delete
    If Not delRng2 Is Nothing Then delRng2.EntireRow.Delete
    
    lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row
    
    y = ws2.Range("A1:D" & lr2).Value
    
    
    Application.ScreenUpdating = True
    End SubOption Explicit
    
    Sub DeleteIdenticalRecordsFromTwoSheets()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim lr1 As Long, lr2 As Long, i As Long
    Dim x, y, xx(), yy(), dict1, dict2
    Dim delRng1 As Range, delRng2 As Range
    
    Application.ScreenUpdating = False
    
    Set ws1 = Worksheets("TAB")
    Set ws2 = Worksheets("Sheet3")
    
    lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row
    
    ws1.Range("A1:D" & lr1).RemoveDuplicates Columns:=Array(1, 2, 3, 4), Header:=xlNo
    
    lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    x = ws1.Range("A1:D" & lr1).Value
    y = ws2.Range("A1:D" & lr2).Value
    
    Set dict1 = CreateObject("Scripting.Dictionary")
    Set dict2 = CreateObject("Scripting.Dictionary")
    
    For i = 1 To UBound(x, 1)
        dict1.Item(x(i, 1) & x(i, 2) & x(i, 3) & x(i, 4)) = ws1.Range("A" & i).Address
    Next i
    
    For i = 1 To UBound(y, 1)
        dict2.Item(y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4)) = ws2.Range("A" & i).Address
    Next i
    
    ws1.Columns("E").Clear
    ws2.Columns("E").Clear
    
    
    
    For i = 1 To UBound(y, 1)
        If dict1.exists(y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4)) Then
            If delRng2 Is Nothing Then
                Set delRng2 = ws2.Range(dict2.Item(y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4)))
            Else
                Set delRng2 = Union(delRng2, ws2.Range(dict2.Item(y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4))))
            End If
        End If
    Next i
    
    
    If Not delRng1 Is Nothing Then delRng1.EntireRow.Delete
    If Not delRng2 Is Nothing Then delRng2.EntireRow.Delete
    
    lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row
    
    y = ws2.Range("A1:D" & lr2).Value
    
    
    Application.ScreenUpdating = True
    End Sub
    Last edited by tweacle; 08-22-2020 at 01:50 PM. Reason: title amendment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Application.EnableEvents not workng correctly code too long? not written correctly?
    By Carissaleigh2010 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-22-2019, 01:13 PM
  2. [SOLVED] Added some columns and Macro Code not working - Re-written?
    By jawnmallon in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-26-2016, 12:19 PM
  3. Macro not working correctly
    By erict in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-30-2010, 10:11 AM
  4. IF Macro not working correctly
    By markrennolds in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2010, 03:22 PM
  5. Macro not working correctly
    By Colin Vicary in forum Excel General
    Replies: 3
    Last Post: 02-21-2007, 04:48 PM
  6. confused as to why macro is not working correctly
    By Amanda Emily in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-02-2006, 01:45 PM
  7. macro not working correctly
    By aaron in forum Excel General
    Replies: 1
    Last Post: 01-23-2006, 12:45 PM

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