why the code run from the second press button?

Mussa

Board Regular
Joined
Jul 12, 2021
Messages
245
Office Version
  1. 2019
  2. 2010
Hello

I modified this code but it shows a problem . when run from the first time will clear the data and when press second will show data
the result should be in sheet2 based on sheet1 it will clear data and brings again from sheet1 this is what happens now but should brings data every time when run from the first time .

VBA Code:
Sub NewEntry_v2()
    Dim OldDict As Object, arrIn As Variant, arrOld As Variant, arrNew() As Variant, a As Long, r As Long
    
    Set OldDict = CreateObject("Scripting.Dictionary")
   
    arrIn = Sheet1.Range("A2", Sheet1.Range("D" & Rows.Count).End(xlUp)).Value
    arrOld = Sheet2.Range("A2", Sheet2.Range("D" & Rows.Count).End(xlUp)).Value

    With OldDict
         .CompareMode = vbTextCompare
        For a = 1 To UBound(arrOld)
            If Not .Exists(arrOld(a, 1)) Then OldDict.Add arrOld(a,1), arrOld(a, 1)
        Next a

        ReDim arrNew(1 To UBound(arrIn), 1 To 4)
        For a = 1 To UBound(arrIn)
            If Not .Exists(arrIn(a, 1)) Then
                r = r + 1
                arrNew(r, 1) = arrIn(a, 1)
                arrNew(r, 2) = arrIn(a, 2)
                arrNew(r, 3) = arrIn(a, 3)
                arrNew(r, 4) = arrIn(a, 4)
                
            End If
        Next a
    End With

    Sheet2.Range("A2").Resize(UBound(arrIn), 4).Value = arrNew


OldDict.RemoveAll
r = 0


    arrIn = Sheet2.Range("A2", Sheet2.Range("D" & Rows.Count).End(xlUp)).Value
    arrOld = Sheet1.Range("A2", Sheet1.Range("D" & Rows.Count).End(xlUp)).Value

    With OldDict

        For a = 1 To UBound(arrOld)
            If .Exists(arrOld(a, 1)) Then OldDict.Add arrOld(a, 1), arrOld(a, 1)
        Next a

        ReDim arrNew(1 To UBound(arrIn), 1 To 4)
        For a = 1 To UBound(arrIn)
            If .Exists(arrIn(a, 1)) Then
                r = r + 1
                arrNew(r, 1) = arrIn(a, 1)
                arrNew(r, 2) = arrIn(a, 2)
                arrNew(r, 3) = arrIn(a, 3)
                arrNew(r, 4) = arrIn(a, 4)
                
            End If
        Next a
    End With

    Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(UBound(arrIn), 4).Value = arrNew
    
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Could you explain what the result should be when you run the code.

Possibly you are trying to keep items in sheet 2 that are not in sheet 1, then add new items from sheet1 to sheet2?
 
Upvote 0
should match based on column A
MS.xlsm
ABCD
1NMIDJOBPART
2MM1-11-ID100ADMIN1PART1
3SD-121-ID101ADMIN2PART2
4MM1-21-ID102ADMIN3PART3
5SD-131-ID103ADMIN4PART4
6MM1-31-ID104ADMIN5PART5
7SD-141-ID105ADMIN6PART6
8SD-151-ID106ADMIN7PART7
sheet1


MS.xlsm
ABCD
1NMIDJOBPART
2MM1-11-ID100ADMIN1PART11
3SSD-151-ID1052ADMIN7PART7
4SD-141-ID1051ADMIN6PART6
5MM1-21-ID10211ADMIN3PART3
6SD-131-ID1031ADMIN4PART4
7SD-121-ID101ADMIN2PART222
8MM1-31-ID1041ADMIN5PART5
sheet2





result based on column B with keep data are in sheet2 but are not existed in sheet1(data could be 6000 rows both two sheets )
MS.xlsm
ABCD
1NMIDJOBPART
2MM1-11-ID100ADMIN1PART1
3SD-121-ID101ADMIN2PART2
4MM1-21-ID102ADMIN3PART3
5SD-131-ID103ADMIN4PART4
6MM1-31-ID104ADMIN5PART5
7SD-141-ID105ADMIN6PART6
8SD-151-ID106ADMIN7PART7
9SSD-151-ID1052ADMIN7PART7
sheet2
 
Upvote 0

Forum statistics

Threads
1,216,038
Messages
6,128,447
Members
449,453
Latest member
jayeshw

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