+ Reply to Thread
Results 1 to 8 of 8

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

Hybrid 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

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,502

    Re: First macro that I have written not working correctly

    Post a sample workbook. See the yellow banner at the top of the screen.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: First macro that I have written not working correctly

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however the thread title does not really convey what your request is about. Tell us what you are trying to do, not how you think it should be done.

    Please take a moment to amend your thread title. Make sure that the title properly explains your request. Your title should be explicit and not be generic (this includes function names used without an indication of what you are trying to achieve).

    Please see Forum Rule #1 about proper thread titles and adjust accordingly. To edit the thread title, open the original post to edit and then click on Go Advanced (bottom right) to access the area where you can edit your title.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    HTH
    Regards, Jeff

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

    Re: macro is showing as data missing but it is not missing

    Please find enclosed workbook with original macro that was written not by me and as you can see it shows stuff as "missing from tableau" on sheet 3 but when you look on sheet called TAB the data is there.

    As stated what I want it to do is run macro as it is now but afterwards check sheet 3 for missing from tableau items and if data is in sheet called "TAB" then I need the line deleting from sheet 3.
    Attached Files Attached Files

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

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

    You need to back up the file first and verify the result...
    Sub test()
        Dim a, i As Long, ii As Long, txt As String, x As Range
        a = Sheets("tab").Cells(1).CurrentRegion.Resize(, 4).Value
        With CreateObject("Scripting.Dictionary")
            For i = 2 To UBound(a, 1)
                For ii = 1 To 4
                    txt = txt & Chr(2) & a(i, ii)
                Next
                .Item(txt) = Empty: txt = ""
            Next
            a = Sheets("sheet3").Cells(1).CurrentRegion.Value
            For i = 2 To UBound(a, 1)
                If a(i, 5) Like "*TAB" Then
                    For ii = 1 To 4
                        txt = txt & Chr(2) & a(i, ii)
                    Next
                    If .exists(txt) Then
                        If x Is Nothing Then
                            Set x = Sheets("sheet3").Rows(i)
                        Else
                            Set x = Union(x, Sheets("sheet3").Rows(i))
                        End If
                    End If
                End If
                txt = ""
            Next
        End With
        If Not x Is Nothing Then x.Delete
    End Sub

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,050

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

    Cross-posted at: https://www.mrexcel.com/board/thread...excel.1143688/
    Please take a few minutes to read the Forum rules at the link below:

    https://www.excelforum.com/forum-rul...rum-rules.html
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

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

    Seems to me your initial comparison code to get the result in Sheet3 in the first place was wrong...Why not address the real expectation instead of fixing holes...
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

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

    tweacle,

    You are welcome and thanks for the rep.

+ 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. 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. [SOLVED] 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. [SOLVED] 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