Avatar of leezac
leezac

asked on 

Formula or VBA to find unmatched cell

In the attached example - I need to be able to do the following:

If Name on MemoryImport sheet does not match Name on Memory sheet then copy the unmatched Name from the MemoryImport sheet to the Memory sheet inserting row or rows starting at row 4 and inserting the unmatched Name into column A.  The other rows on the Memory tab have formulas and will need to retain the cell references for those also.


Thanks in advance.
onlist.zip
Microsoft Excel

Avatar of undefined
Last Comment
Zack Barresse
Avatar of Zack Barresse
Zack Barresse
Flag of United States of America image

Hi there,

If I understood you right, perhaps this is what you want..

Sub CopyUnmatchedHostNames()
    
    Dim wsMemoryImport As Worksheet
    Dim wsMemory As Worksheet
    Dim rCheck As Range
    Dim rCell As Range
    Dim iCheck As Long
    Dim iCnt As Long
    
    Set wsMemoryImport = ThisWorkbook.Worksheets("MemoryImport")
    Set wsMemory = ThisWorkbook.Worksheets("Memory")
    With wsMemoryImport
        Set rCheck = .Range("A4", .Cells(.Rows.Count, 1).End(xlUp))
    End With
    For Each rCell In rCheck.Cells
        On Error GoTo NoMatch
        iCheck = 0
        iCheck = WorksheetFunction.Match(rCell.Value, wsMemory.Range("D:D"), 0)
        GoTo MatchFound
NoMatch:
        On Error Resume Next
        wsMemory.Rows(4).Copy
        wsMemory.Rows(4).Insert
        wsMemory.Range("A4:C4").ClearContents
        wsMemory.Range("D4").Value = rCell.Value
        iCnt = iCnt + 1
MatchFound:
        On Error GoTo 0
    Next rCell
    
    If iCnt > 0 Then
        MsgBox iCnt & " total record(s) were copied.", vbExclamation, "DONE!"
    Else
        MsgBox "No Host Names copied.", vbExclamation, "DONE!"
    End If
    
End Sub

Open in new window


This will go through the MemoryImport sheet looking at the Host Name (col A) looking for matches in the Memory sheet in column D (Host Name). If any values are not found in the Memory sheet it will insert a row at row 4, clear cols A through C of that row and populate D4 with the Host Name. There is a message box telling you how many were copied, if at all.

HTH
Regards,
Zack Barresse
Avatar of leezac
leezac

ASKER

Looks exactly what I need, but if there is a second mismatch on Memory Import - I get an

error on this line

iCheck = WorksheetFunction.Match(rCell.Value, wsMemory.Range("D:D"), 0)

I took out

iCheck = WorksheetFunction.Match(rCell.Value, wsMemory.Range("D:D"), 0)
GoTo MatchFound

since I do not need to find matches and it works good now without error and copies the values I need to the memory tab.
Avatar of leezac
leezac

ASKER

Well I probably need the code I took out it seems, not sure how to fix though.
ASKER CERTIFIED SOLUTION
Avatar of Zack Barresse
Zack Barresse
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of leezac
leezac

ASKER

I added the last On Error Resume and it seems to work... Thanks...
Avatar of Zack Barresse
Zack Barresse
Flag of United States of America image

Very welcome, glad it works for you!

Zack
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo