abdelfattah
Well-known Member
- Joined
- May 3, 2019
- Messages
- 1,494
- Office Version
- 2019
- 2010
- Platform
- Windows
Hi
I have problem in this code . it deletes items from sheet ITEMS because of they are not existed in sheet1
I want keeping new items in sheet ITEMS even if they are not existed in sheet1
the code should match column B between two sheets and pull the values from columns C,D from sheet1 to sheet ITEMS and if there is new item in sheet1 but is not existed in sheet ITEMS , then should add to sheet items. the only problem as I said before , it deletes items from sheet ITEMS because of they are not existed in sheet1 , shouldn't !
I hope finding assistance from experts.
I have problem in this code . it deletes items from sheet ITEMS because of they are not existed in sheet1
I want keeping new items in sheet ITEMS even if they are not existed in sheet1
the code should match column B between two sheets and pull the values from columns C,D from sheet1 to sheet ITEMS and if there is new item in sheet1 but is not existed in sheet ITEMS , then should add to sheet items. the only problem as I said before , it deletes items from sheet ITEMS because of they are not existed in sheet1 , shouldn't !
I hope finding assistance from experts.
VBA Code:
Sub test1()
Dim ws As Worksheet, a, i As Long, w, dic As Object
Set dic = CreateObject("Scripting.Dictionary")
Set ws = Sheets("SHEET1")
With ws
a = ws.Cells(1).CurrentRegion.Value
For i = 2 To UBound(a, 1)
If a(i, 2) <> "" Then
If Not dic.exists(a(i, 2)) Then
ReDim w(1 To 4)
w(2) = a(i, 2)
Else
w = dic(a(i, 2))
End If
w(3) = a(i, 3): w(4) = a(i, 4)
dic(a(i, 2)) = w
End If
Next
End With
With Sheets("ITEMS").Cells(1).CurrentRegion
.Offset(1).ClearContents
If dic.Count Then
With .Rows(2).Resize(dic.Count)
.Value = Application.Index(dic.items, 0, 0)
.Columns(1) = Evaluate("row(1:" & .Rows.Count & ")")
End With
End If
End With
End Sub