Sub flavorTOWN()
Dim ws As Worksheet, os As Worksheet
Dim wsCOUNT As Long, lastRow As Long
Dim i As Long, x As Long, j As Long, p As Long
Dim flSELECT(), loSELECT(), sSheet As Variant
Dim sumFLAV As Variant
Dim dicLOCATION As Object, dicFLAVOR As Object
'set variables
Set os = Sheets("Master List")
lastRow = os.Range("A" & Rows.Count).End(xlUp).Row
Set dicLOCATION = CreateObject("scripting.dictionary")
Set dicFLAVOR = CreateObject("scripting.dictionary")
x = 0
'pass all selected flavors into array
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
ReDim Preserve flSELECT(x)
flSELECT(x) = ListBox1.List(i)
x = x + 1
End If
Next i
x = 0
'pass all selected locations into array
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) = True Then
ReDim Preserve loSELECT(x)
loSELECT(x) = ListBox2.List(i)
x = x + 1
End If
Next i
'size of the sums array
ReDim sumFLAV(LBound(flSELECT) To UBound(flSELECT), LBound(loSELECT) To UBound(loSELECT))
'use arrays to get sums
For Each ws In Sheets
If ws.Name <> "Master List" Then
'or ws.name <>"OTHERSHEETS" then
sSheet = ws.Range("A1").CurrentRegion.Value2
For i = LBound(sSheet) To UBound(sSheet)
For x = LBound(loSELECT) To UBound(loSELECT)
If sSheet(i, 3) = loSELECT(x) Then
For p = LBound(flSELECT) To UBound(flSELECT)
If sSheet(i, 1) = flSELECT(p) Then
sumFLAV(p, x) = sumFLAV(p, x) + sSheet(i, 4)
End If
Next p
End If
Next x
Next i
End If
Next ws
'paste results to sheet
x = 2
For j = LBound(loSELECT) To UBound(loSELECT)
os.Range("D" & x).Value = loSELECT(j)
For p = LBound(flSELECT) To UBound(flSELECT)
os.Range("E" & x).Value = flSELECT(p)
os.Range("F" & x).Value = sumFLAV(p, j)
x = x + 1
Next p
Next j
End Sub
Private Sub UserForm_Activate()
Dim ws As Worksheet
Dim flARY As Variant, loARY As Variant
Set ws = Sheets("Master List")
lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
flARY = ws.Range("A2:A" & lastRow).Value2
loARY = ws.Range("B2:B" & lastRow).Value2
ListBox1.List = flARY
ListBox2.List = loARY
End Sub
Private Sub CommandButton1_Click()
Call flavorTOWN
Unload UserForm1
End Sub