MS Excel VBA Function Not Updating Sheet when duplicates in Source Sheet

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for your assistance. I took the awesome work by mumps in the following code and modified it to my needs.

I am having a couple of issues and would like to ask for assistance.

1. How do I modify the function so if the sheet (referred to as Sheets(ShtNmUpdt) in the function and "Sheet1" of the Sub) has duplicates values, it still includes everything after the duplicate value. For example, once it sees Celtics twice, it stops adding to the list and only up to the first "Celtics", it gets updated. I believe the section of the code that needs to be modified is within the function:

VBA Code:
    With Sheets(ShtNmOrgl)
        For Each Rng In .Range(AdrsOrgl, .Cells(.Rows.Count, ColNoOrgl).End(xlUp))
            If Not RngList.Exists(Rng.Value) Then
                 RngList.Add Rng.Value, Nothing     '
            End If
        Next
    End With


2. If a value was not in Sheets(ShtNmOrgl) ("Sheet2" of the sub), I would like to shade that value in Sheets(ShtNmUpdt) ("Sheet1" of the sub). It could be this line of code:
VBA Code:
aDicMisgVal.Add Rng.Value, Nothing

in this section of the function:
VBA Code:
   With Sheets(ShtNmUpdt)
        For Each Rng In .Range(AdrsUpdt, .Cells(.Rows.Count, ColNoUpdt).End(xlUp))
            If aDictionary.Exists(Rng.Value) Then
                'do nothing so it gets excluded from the list
            ElseIf Not RngList.Exists(Rng.Value) Then
                aDicMisgVal.Add Rng.Value, Nothing
                Sheets(ShtNmOrgl).Cells(Sheets(ShtNmOrgl).Rows.Count, ColNoOrgl).End(xlUp).Offset(1, 0) = Rng
            End If
        Next
    End With

and or the following code:
VBA Code:
    With Sheets(ShtNmUpdt)
        For Each Rng In .Range(AdrsOrgl, .Cells(.Rows.Count, ColNoOrgl).End(xlUp))
            If ColrMisgVal = "Yes" And aDicMisgVal(Rng.Val).Exists Then
                Rng.Interior.ColorIndex = 38
            End If
        Next
    End With

Here is the "Sheet2" (name within the sub) which is Sheets(ShtNmOrgl) within the function before any updates:
Book1
ABC
1
2TEAM
3Lakers
4Warriors
5
6
7
Sheet2


Here is the "Sheet1" (name within the sub) which is Sheets(ShtNmOrgl). The sheet with the updates. Basically, all values in "Sheet2" that are not in "Sheet1", will be added to "Sheet1" and then color them/shade the cells in "Sheet2"
Book1
ABCD
1
2TEAM
3Warriors
4Rockets
5Mavericks
6Celtics
7Celtics
8Pistons
9
Sheet1


"Sheet2" actual results
Book1
ABC
1
2TEAM
3Lakers
4Warriors
5Rockets
6Mavericks
7Celtics
8
Sheet2


"Sheet2" desired results
Book1
ABC
1
2TEAM
3Lakers
4Warriors
5Rockets
6Mavericks
7Celtics
8Pistons
9
Sheet2


"Sheet1" shading to show what was updated in "Sheet2"
Book1
ABCD
1
2TEAM
3Warriors
4Rockets
5Mavericks
6Celtics
7Celtics
8Pistons
9
10
Sheet1


The sub:
VBA Code:
Sub CompareTest()

    Dim aDictionary As Object

    Set aDictionary = CreateObject("Scripting.Dictionary") 'nothing is stored in the dictionary to be excluded in this version
    CmprListsNAddF "Sheet2", "Sheet1", "TEAM", aDictionary, "Yes"

End Sub

The function:
VBA Code:
Function CmprListsNAddF(ShtNmOrgl As String, ShtNmUpdt As String, ColHdgNm As String, _
                         aDictionary As Object, ColrMisgVal As String) As Variant

'Notes
    'ShtNmOrgl as String - The sheet with the original data which will be updated
    'ShtNmUpdt as String - The sheet with the data with updates. It will be transferred to the ShtNmOrgl
    'ColHdgNm as String - The name of the column Heading with the data to be updated
    'aDictionary As Object - The values to be excluded from the list. _
        This needs to be declared in the Sub where, for example, "aDictionary" is setting, the following _
        needs to be done: _
        Dim aDictionary as Object _
        Set aDictionary = CreateObject("Scripting.Dictionary") _
        Then read the values if any into aDictionary. If there are none, it can be left blank. Two examples: _
            (1)aDictionary("MARK") = Empty _
            (2) For j = 2 To 6 _
                    aDictionary(.Cells(j, 1).Value) = Empty -> in this example, 1 is the column no. _
                Next j _
            basically in these examples what's in the parethesis is the value being stored in the _
            dictionary, which will be exlcuded to be added

 '_______________________________________________________________________________________________________________
 'Turn off alerts, screen updates, and automatic calculation
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Application.Calculation = xlManual           
            
 '_______________________________________________________________________________________________________________
 'Dimensioning
  
    'Dim longs
     Dim LastRow As Long
     
     Dim RowNoOrgl As Long
     Dim ColNoOrgl As Long
     
     Dim RowNoUpdt As Long
     Dim ColNoUpdt As Long
     
     Dim RowNo As Long
     Dim ColNo As Long

    
    'Dim Strings
     Dim AdrsOrgl As String
     Dim ColLetOrgl As String
     Dim AdrsUpdt As String
     Dim ColLetUpdt As String
     
     Dim ErrMsg1 As String
     Dim ErrMsg2 As String
     
     
    'Dim Ranges
     Dim Rng As Range
     
     
    'Dim Objects
     Dim RngList As Object
     Dim aDicMisgVal As Object
     
    
    'Dim Variants
     Dim ColHdgNmOrgl As Variant
     Dim ColHdgNmUpdt As Variant
    
    
    'Dim Timer variables
     Dim TimerCount As Long
     Dim BenchMark As Double
 
 
 '______________________________________________________________________________________________________________
    
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    Set RngList = CreateObject("Scripting.Dictionary")
    
    Set aDicMisgVal = CreateObject("Scripting.Dictionary")



 '______________________________________________________________________________________________________________

    With Sheets(ShtNmOrgl)
        
        On Error GoTo 1000
        Set ColHdgNmOrgl = .Cells.Find(what:=ColHdgNm, LookIn:=xlFormulas, LookAt _
                :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                False, SearchFormat:=False)
        
        If ColHdgNmOrgl Is Nothing Then
            ErrMsg1 = "Yes"
            GoTo 1000
        Else
            AdrsOrgl = ColHdgNmOrgl.Address
            RowNoOrgl = ColHdgNmOrgl.Row
            ColNoOrgl = ColHdgNmOrgl.Column
        End If
        
    End With
        
 
 
 '______________________________________________________________________________________________________________

1000:
    With Sheets(ShtNmUpdt)
        
        On Error GoTo 2000
        Set ColHdgNmUpdt = .Cells.Find(what:=ColHdgNm, LookIn:=xlFormulas, LookAt _
                :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                False, SearchFormat:=False)
        
        If ColHdgNmOrgl Is Nothing Then
            ErrMsg2 = "Yes"
        Else
            AdrsUpdt = ColHdgNmUpdt.Address
            RowNoUpdt = ColHdgNmUpdt.Row
            ColNoUpdt = ColHdgNmUpdt.Column
        End If

    End With
        
        
 '______________________________________________________________________________________________________________
    
    With Sheets(ShtNmOrgl)
        For Each Rng In .Range(AdrsOrgl, .Cells(.Rows.Count, ColNoOrgl).End(xlUp))
            If Not RngList.Exists(Rng.Value) Then
                 RngList.Add Rng.Value, Nothing 
            End If
        Next
    End With
 
 
 
 '______________________________________________________________________________________________________________
 
    With Sheets(ShtNmUpdt)
        For Each Rng In .Range(AdrsUpdt, .Cells(.Rows.Count, ColNoUpdt).End(xlUp))
            If aDictionary.Exists(Rng.Value) Then
                'do nothing so it gets excluded from the list
            ElseIf Not RngList.Exists(Rng.Value) Then 'if it is not already in the sheet, add it.
                aDicMisgVal.Add Rng.Value, Nothing 'this stores all the values that were not in the list
                Sheets(ShtNmOrgl).Cells(Sheets(ShtNmOrgl).Rows.Count, ColNoOrgl).End(xlUp).Offset(1, 0) = Rng
            End If
        Next
    End With
    
    
    
 '________________________________________________________________________________________________________
 'Code - This goes through the sheet ShtNmOrgl (the sheet with the missing values in Sheets(ShtNmUpdt) _
         which were updated) and colors them if indicated.
    
    With Sheets(ShtNmUpdt)
        For Each Rng In .Range(AdrsOrgl, .Cells(.Rows.Count, ColNoOrgl).End(xlUp))
            If ColrMisgVal = "Yes" And aDicMisgVal(Rng.Val).Exists Then
                Rng.Interior.ColorIndex = 38
            End If
        Next
    End With
 
 
 
 '________________________________________________________________________________________________________
 'Code -
    
    RngList.RemoveAll
 
 

 '______________________________________________________________________________________________________________
 'Code -
    
2000:

    If ErrMsg1 = "Yes" And ErrMsg2 = "Yes" Then
        MsgBox "There is an issue with both the Original and Update data."
    ElseIf ErrMsg1 = "Yes" Then
        MsgBox "There is an issue with the Original data."
    ElseIf ErrMsg2 = "Yes" Then
        MsgBox "There is an issue with the Update data."
    End If
    
    



 '_________________________________________________________________________________________________________________
 'Turn on alerts and screen updates, and calculate
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Calculate



 '_________________________________________________________________________________________________________________
 'End of the subroutine/macro
 
 
 
End Function
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top