I've updated two modules. Let me know how it works.
Private Sub Worksheet_Change(ByVal Target As Range)
'adds new entry fields as item to summary page. Consolidates new data with full summary
Dim k&, i&, j&, jn$, cl&, jnc$, jns$, lr&, lrs&, ii&, jj&
Dim t&, y&, u&, r&, lrk&, g&, lrl&, f&, s&, c&, lrc&, lrcc&
Dim OutWB As Workbook
Dim su As Worksheet
Dim a, b$()
Dim dict As Object, dic As Object
Dim posit As Range, data As Range
Application.EnableEvents = False
Application.ScreenUpdating = False
If Not Intersect(Target, Range("E:E")) Is Nothing Then
If Target.Cells.Count > 1 Then GoTo ext
Set OutWB = ThisWorkbook
Set dict = CreateObject("Scripting.Dictionary") ' Initial summary items
Set dic = CreateObject("Scripting.Dictionary") ' full summary items
Set su = OutWB.Sheets("SUMMARY")
If IsEmpty(Target.Offset(, -3)) Or IsEmpty(Target.Offset(, -2)) Or IsEmpty(Target.Offset(, -1)) Then
'sts = False
GoTo ext
End If
lr = su.Cells(Rows.Count, 2).End(xlUp).Row + 20
k = 1
lrs = WorksheetFunction.RoundDown((lr - 4) / 3, 0)
ReDim b(1 To lrs, 1 To 9)
For i = 5 To lr Step 10
For j = 3 To 9 Step 3
jns = Join(Array(su.Cells(i, j), su.Cells(i + 1, j), su.Cells(i + 2, j)), "")
If Not dict.exists(jns) Then
dict.Add jns, k
b(k, 1) = jns
b(k, 2) = su.Cells(i, j)
b(k, 3) = su.Cells(i + 1, j)
b(k, 4) = su.Cells(i + 2, j)
k = k + 1
End If
Next j
Next i
jnc = Join(Array(Target.Offset(, -3), Target.Offset(, -2), Target.Offset(, -1)), "")
'if an item does show up in the full summary it must show those results
'Check if item is in full summary
'Collect full summary into array
Call sumrizegrpsscrtch
Application.EnableEvents = False
Application.ScreenUpdating = False
lrc = su.Cells(Rows.Count, 2).End(xlUp).Row + 20
lrcc = WorksheetFunction.RoundDown((lrc - 4) / 3, 0)
ReDim a(1 To lrcc, 1 To 9)
k = 1
For i = 5 To lrc Step 10
For j = 3 To 9 Step 3
jns = Join(Array(su.Cells(i, j), su.Cells(i + 1, j), su.Cells(i + 2, j)), "")
If Not dic.exists(jns) Then
dic.Add jns, k
a(k, 1) = jns
a(k, 2) = su.Cells(i, j)
a(k, 3) = su.Cells(i + 1, j)
a(k, 4) = su.Cells(i + 2, j)
a(k, 5) = su.Cells(i + 3, j)
a(k, 6) = su.Cells(i + 4, j)
a(k, 7) = su.Cells(i + 5, j)
a(k, 8) = su.Cells(i + 6, j)
a(k, 9) = su.Cells(i + 7, j)
k = k + 1
End If
Next j
Next i
'Update items that exists with full summary data
For ii = LBound(b) To UBound(b)
For jj = LBound(a) To UBound(a)
If b(ii, 1) = a(jj, 1) Then
b(ii, 4) = a(jj, 4)
b(ii, 5) = a(jj, 5)
b(ii, 6) = a(jj, 6)
b(ii, 7) = a(jj, 7)
b(ii, 8) = a(jj, 8)
b(ii, 9) = a(jj, 9)
End If
Next jj
Next ii
'Clear summary page
lrk = su.Cells(Rows.Count, 2).End(xlUp).Row
su.Range("B5:I" & lrk).ClearContents
su.Range("B5:I" & lrk).ClearFormats
'Add initial items back to summary page
k = 1
For y = 5 To lr Step 10
For u = 3 To 9 Step 3
su.Cells(y, u).Value = b(k, 2)
su.Cells(y + 1, u).Value = b(k, 3)
su.Cells(y + 2, u).Value = b(k, 4)
su.Cells(y + 3, u).Value = b(k, 5)
su.Cells(y + 4, u).Value = b(k, 6)
su.Cells(y + 5, u).Value = b(k, 7)
su.Cells(y + 6, u).Value = b(k, 8)
su.Cells(y + 7, u).Value = b(k, 9)
k = k + 1
Next u
Next y
'if item doesn't exist in initial summary
If Not dict.exists(jnc) Then
'find next open position
For y = 5 To lr Step 10
For u = 3 To 9 Step 3
If IsEmpty(su.Cells(y, u)) Then
Set posit = su.Cells(y, u)
GoTo fin
End If
r = r + 1
Next u
Next y
fin:
'if not in full summary then add new zero'd item to summary page
If Not dic.exists(jnc) Then
r = r + 1
su.[B5:C13].Copy su.Cells(posit.Row, posit.Column - 1)
su.Range(posit.Offset(3), posit.Offset(8)).Value = 0
posit.Value = Target.Offset(, -3).Value
posit.Offset(1).Value = Target.Offset(, -2)
posit.Offset(2).Value = Target.Offset(, -1)
'if item in full summary and not in initial page, pull that item into current summary page
Else
For k = LBound(a) To UBound(a)
If jnc = a(k, 1) Then
su.Cells(y, u).Value = a(k, 2)
su.Cells(y + 1, u).Value = a(k, 3)
su.Cells(y + 2, u).Value = a(k, 4)
su.Cells(y + 3, u).Value = a(k, 5)
su.Cells(y + 4, u).Value = a(k, 6)
su.Cells(y + 5, u).Value = a(k, 7)
su.Cells(y + 6, u).Value = a(k, 8)
su.Cells(y + 7, u).Value = a(k, 9)
End If
Next k
End If
End If
'Reformat items
lrl = su.Cells(Rows.Count, 3).End(xlUp).Row
For y = 5 To lrl Step 10
For u = 3 To 9 Step 3
If su.Cells(y, u).Interior.Pattern = xlNone And Not IsEmpty(su.Cells(y, u)) Then
su.Range(su.Cells(y, u - 1), su.Cells(y + 8, u - 1)).Font.Color = 16777215
su.Range(su.Cells(y, u - 1), su.Cells(y + 7, u - 1)).Interior.Color = 13998939
su.Range(su.Cells(y, u), su.Cells(y + 7, u)).Interior.Color = 16247773
su.Cells(y + 8, u - 1).Interior.Color = 255
su.Cells(y + 8, u).Interior.Color = 1137094
su.Range(su.Cells(y, u - 1), su.Cells(y + 8, u - 1)).Font.Bold = True
su.Range(su.Cells(y, u - 1), su.Cells(y + 8, u - 1)).Value = Application.Transpose(Array("BRAND", "TYPE", "ORIGIN", "FIRST", "IMPORT", "EXPORT", "RETURNS 1", "RETURNS 2", "BALANCE"))
su.Cells(y + 8, u).FormulaR1C1 = "=R[-5]C+R[-4]C-R[-3]C-R[-2]C+R[-1]C"
With su.Range(su.Cells(y, u - 1), su.Cells(y + 8, u))
.Borders(7).LineStyle = xlContinuous
.Borders(7).Weight = xlThin
.Borders(8).LineStyle = xlContinuous
.Borders(8).Weight = xlThin '
.Borders(9).LineStyle = xlContinuous
.Borders(9).Weight = xlThin
.Borders(10).LineStyle = xlContinuous
.Borders(10).Weight = xlThin
.Borders(11).LineStyle = xlContinuous
.Borders(11).Weight = xlThin
.Borders(12).LineStyle = xlContinuous
.Borders(12).Weight = xlThin
End With
su.Range(su.Cells(y, u), su.Cells(y + 8, u)).HorizontalAlignment = xlCenter
End If
su.Cells(y + 8, u).Value = su.Cells(y + 8, u).Value
Next u
Next y
ext:
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
and
Sub CheckSelExists()
'Checks if combination of validation fields exist as a table.
'Enters combination into the Fourth tab and summary tab if doesn't exist
Dim k&, i&, j&, jn$, cl&, jnc$, jns$, lr&, lrs&, lrc&, lrcc&
Dim t&, y&, u&, r&, lrk&, g&, lri&, lrl&, lc&, f&, s&
Dim OutWB As Workbook
Dim su As Worksheet
Dim a, b$(), d, dt
Dim dict As Object
Dim posit As Range, posit2 As Range, data As Range
Application.EnableEvents = False
Application.ScreenUpdating = False
Set OutWB = ThisWorkbook
Set dict = CreateObject("Scripting.Dictionary")
Set su = OutWB.Sheets("SUMMARY")
If IsEmpty(su.[C2]) Or IsEmpty(su.[D2]) Or IsEmpty(su.[E2]) Then
'MsgBox "Atleast one selection is empty"
Call sumrizegrpsscrtch
sts = False
GoTo ext
End If
lri = su.Cells(Rows.Count, 2).End(xlUp).Row
lc = su.Cells(5, Columns.Count).End(xlToLeft).Column
jnc = Join(Array(su.[C2], su.[D2], su.[E2]), "")
If sts = True Then
d = su.Range(su.[B5], su.Cells(lri, lc)).Value
'Check if selection already exists
lrc = su.Cells(Rows.Count, 2).End(xlUp).Row + 20
k = 1
lrcc = WorksheetFunction.RoundDown((lrc - 4) / 3, 0)
ReDim b(1 To lrcc, 1 To 9)
For i = 5 To lrc Step 10
For j = 3 To 9 Step 3
jns = Join(Array(su.Cells(i, j), su.Cells(i + 1, j), su.Cells(i + 2, j)), "")
If Not dict.exists(jns) Then
dict.Add jns, k
b(k, 1) = jns
b(k, 2) = su.Cells(i, j)
b(k, 3) = su.Cells(i + 1, j)
b(k, 4) = su.Cells(i + 2, j)
k = k + 1
If jns = jnc Then GoTo ext
End If
Next j
Next i
End If
Call sumrizegrpsscrtch
Application.EnableEvents = False
Application.ScreenUpdating = False
lr = su.Cells(Rows.Count, 2).End(xlUp).Row + 20
With dict
k = 1
lrs = WorksheetFunction.RoundDown((lr - 4) / 3, 0)
For i = 5 To lr Step 10
For j = 3 To 9 Step 3
jns = Join(Array(su.Cells(i, j), su.Cells(i + 1, j), su.Cells(i + 2, j)), "")
If Not .exists(jns) Then
.Add jns, k
End If
k = k + 1
Next j
Next i
If .exists(jnc) Then
'lr = lr - 10
For y = 5 To lr Step 10
For u = 3 To 9 Step 3
If jnc = Join(Array(su.Cells(y, u), su.Cells(y + 1, u), su.Cells(y + 2, u)), "") Then
dt = su.Range(su.Cells(y, u - 1), su.Cells(y + 8, u))
su.Range("B5:I" & lr).ClearContents
su.Range("B5:I" & lr).ClearFormats
su.Range("E5:I13").ClearContents
su.Range("E5:I13").ClearFormats
GoTo leav
End If
Next
Next
leav:
su.Range(su.[B5], su.Cells(lri, lc)).Value = d
For f = 5 To lr Step 10
For s = 3 To 9 Step 3
If IsEmpty(su.Cells(f, s)) Then
Set posit2 = su.Cells(f, s)
GoTo mve
End If
r = r + 1
Next
Next
mve:
su.Range(su.Cells(f, s - 1), su.Cells(f + 8, s)).Value = dt
Else
With Sheets("FOURTH")
For g = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
If jnc = Join(Array(.Cells(g, 2), .Cells(g, 3), .Cells(g, 4)), "") Then
GoTo skip
End If
Next
lrk = .Cells(Rows.Count, 1).End(xlUp).Row + 1
Application.EnableEvents = False
.Cells(lrk, 1).Value = .Cells(lrk - 1, 1).Value + 1
.Cells(lrk, 2).Value = su.[C2].Value
.Cells(lrk, 3).Value = su.[D2].Value
.Cells(lrk, 4).Value = su.[E2].Value
.Cells(lrk, 5).Value = 0
End With
skip:
su.Range("B5:I" & lr).ClearContents
su.Range("B5:I" & lr).ClearFormats
If IsEmpty(d) Then
d = su.[B5:C13].Value
Else
su.Range("B5", Cells(lri, lc)).Value = d
End If
For y = 5 To lr Step 10
For u = 3 To 9 Step 3
If IsEmpty(su.Cells(y, u)) Then
Set posit = su.Cells(y, u)
GoTo fin
End If
r = r + 1
Next
Next
fin:
r = r + 1
su.[B5:C13].Copy su.Cells(posit.Row, posit.Column - 1)
posit.Value = su.[C2].Value
posit.Offset(1).Value = su.[D2].Value
posit.Offset(2).Value = su.[E2].Value
su.Range(posit.Offset(3), posit.Offset(8)).Value = 0
End If
End With
lrl = su.Cells(Rows.Count, 3).End(xlUp).Row
For y = 5 To lrl Step 10
For u = 3 To 9 Step 3
If Cells(y, u).Interior.Pattern = xlNone And Cells(y, u) <> "" Then
su.Range(su.Cells(y, u - 1), su.Cells(y + 8, u - 1)).Font.Color = 16777215
su.Range(su.Cells(y, u - 1), su.Cells(y + 7, u - 1)).Interior.Color = 13998939
su.Range(su.Cells(y, u), su.Cells(y + 7, u)).Interior.Color = 16247773
su.Cells(y + 8, u - 1).Interior.Color = 255
su.Cells(y + 8, u).Interior.Color = 1137094
su.Range(su.Cells(y, u - 1), su.Cells(y + 8, u - 1)).Font.Bold = True
su.Range(su.Cells(y, u - 1), su.Cells(y + 8, u - 1)).Value = Application.Transpose(Array("BRAND", "TYPE", "ORIGIN", "FIRST", "IMPORT", "EXPORT", "RETURNS 1", "RETURNS 2", "BALANCE"))
su.Cells(y + 8, u).FormulaR1C1 = "=R[-5]C+R[-4]C-R[-3]C-R[-2]C+R[-1]C"
With su.Range(su.Cells(y, u - 1), su.Cells(y + 8, u))
.Borders(7).LineStyle = xlContinuous
.Borders(7).Weight = xlThin
.Borders(8).LineStyle = xlContinuous
.Borders(8).Weight = xlThin '
.Borders(9).LineStyle = xlContinuous
.Borders(9).Weight = xlThin
.Borders(10).LineStyle = xlContinuous
.Borders(10).Weight = xlThin
.Borders(11).LineStyle = xlContinuous
.Borders(11).Weight = xlThin
.Borders(12).LineStyle = xlContinuous
.Borders(12).Weight = xlThin
End With
su.Range(su.Cells(y, u), su.Cells(y + 8, u)).HorizontalAlignment = xlCenter
End If
su.Cells(y + 8, u).Value = su.Cells(y + 8, u).Value
Next u
Next y
sts = True
Set dict = Nothing
ext:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Bookmarks