Sub UpdateSheet2x()
Dim LR As Long, c As Range, k&, i&, j&, nx&, x&, r&, n&, xx&
Dim s1 As Worksheet, s2 As Worksheet
Dim d As Object, dd As Object, a
Set s1 = Sheets("sheet1")
Set s2 = Sheets("sheet2")
Application.ScreenUpdating = False
a = s1.[A1].Offset(1).CurrentRegion
n = s2.Range("B" & s2.Rows.Count).End(xlUp).Row
For i = n To 1 Step -1
If s2.Cells(i, 1).Interior.ColorIndex = 3 Then '3 = Red
s2.Rows(i).Delete
End If
Next
xx = s2.Range("B" & s2.Rows.Count).End(xlUp).Row
Set d = CreateObject("Scripting.Dictionary")
Set dd = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
For i = LBound(a) To UBound(a)
If Not d.exists(a(i, 2)) Then
d.Add a(i, 2), k
k = k + 1
End If
Next i
For i = LBound(a) To UBound(a)
For x = 2 To xx
If Not dd.exists(s2.Cells(x, 2).Value) Then
dd.Add s2.Cells(x, 2).Value, r
r = r + 1
End If
If s2.Cells(x, 2).Value = a(i, 2) Then
s2.Cells(x, 5).Value = s2.Cells(x, 3).Value - a(i, 3)
If s2.Cells(x, 5).Value = 0 Then
s2.Cells(x, 4).Value = Chr(252)
Else
s2.Cells(x, 4).Value = Chr(251)
End If
s2.Cells(x, 4).Font.Name = "Wingdings"
x = x + 1
End If
Next
Next
nx = xx + 1
For j = 2 To UBound(a)
If Not dd.exists(a(j, 2)) Then 'And s2.Cells(nx, 1).Interior.ColorIndex <> 3 Then
s2.Cells(nx, 1).Value = s2.Cells(nx - 1, 1).Value + 1
s2.Cells(nx, 2).Value = a(j, 2)
s2.Cells(nx, 3).Value = a(j, 3)
s2.Cells(nx, 4).Value = Chr(251)
s2.Cells(nx, 4).Font.Name = "Wingdings"
s2.Cells(nx, 5).Value = -a(j, 3)
s2.Range("A" & nx & ":E" & nx).Interior.ColorIndex = 3 ' red 10 ' green
s2.Range("A" & nx & ":E" & nx).Borders.Weight = xlThin
s2.Range("A" & nx & ":D" & nx).HorizontalAlignment = xlCenter
nx = nx + 1
End If
Next j
For x = 2 To nx - 1
If IsEmpty(s2.Cells(x, 5)) Or s2.Range("A" & x & ":E" & x).Interior.ColorIndex = 43 Then
s2.Cells(x, 4).Value = Chr(251)
s2.Cells(x, 4).Font.Name = "Wingdings"
s2.Cells(x, 5).Value = s2.Cells(x, 3).Value
s2.Range("A" & x & ":E" & x).Interior.ColorIndex = 43 ' lt green
End If
Next
Application.ScreenUpdating = True
End Sub
Bookmarks