Option Explicit
Sub test()
Dim x, msg As String, dic As Object
GetData ThisWorkbook.Path, x, msg
If Not IsArray(x) Then MsgBox "No files in the forlder": Exit Sub
If Len(msg) Then MsgBox msg: Exit Sub
Set dic = CreateObject("Scripting.Dictionary")
GetDic x, dic
OutPut "Before", x, dic
Set dic = Nothing
End Sub
Private Sub GetData(myDir As String, x, msg As String)
Dim fn As String, cn As Object, rs As Object
fn = Dir(ThisWorkbook.Path & "\*.xls")
If fn = ThisWorkbook.Name Then fn = Dir()
If fn = "" Then msg = "No file": Exit Sub
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
With cn
.Provider = "Microsoft.Ace.OLEDB.12.0"
.Properties("Extended Properties") = "Excel 12.0;HDR=Yes;"
.Open ThisWorkbook.Path & "\" & fn
End With
Do While fn <> ""
If fn <> ThisWorkbook.Name Then
If IsEmpty(x) Then
ReDim x(1 To 1)
Else
ReDim Preserve x(1 To UBound(x) + 1)
End If
On Error Resume Next
rs.Open "Select `CATOGERY`, `BRAND`, `TYPE`, `MONAFACTURE`, Sum(`Purchase`), Sum(`Sales`) From `PR$` " & _
"In '" & ThisWorkbook.Path & "\" & fn & "' 'Excel 12.0;''HDR:=Yes;''' Group By `CATOGERY`, `BRAND`, `TYPE`, `MONAFACTURE`;", cn, 3
If Err.Number <> 0 Then msg = fn & " has problem": Exit Do
On Error GoTo 0
x(UBound(x)) = rs.GetRows: rs.Close
End If
fn = Dir
Loop
Set cn = Nothing: Set rs = Nothing
End Sub
Private Sub GetDic(x, dic As Object)
Dim i As Long, ii As Long, txt As String, w
For i = 1 To UBound(x)
For ii = 0 To UBound(x(i), 2)
If Not dic.exists(x(i)(0, ii)) Then
Set dic(x(i)(0, ii)) = CreateObject("Scripting.Dictionary")
End If
txt = Join(Array(x(i)(1, ii), x(i)(2, ii)), "")
x(i)(4, ii) = IIf(IsNull(x(i)(4, ii)), 0, x(i)(4, ii))
x(i)(5, ii) = IIf(IsNull(x(i)(5, ii)), 0, x(i)(5, ii))
If Not dic(x(i)(0, ii)).exists(txt) Then
dic(x(i)(0, ii))(txt) = Array(x(i)(1, ii), x(i)(2, ii), x(i)(3, ii), x(i)(4, ii), x(i)(5, ii))
Else
w = dic(x(i)(0, ii))(txt): w(3) = w(3) + x(i)(4, ii)
w(4) = w(4) + x(i)(5, ii): dic(x(i)(0, ii))(txt) = w
End If
Next
Next
End Sub
Private Sub OutPut(wsName As String, x, dic As Object)
Dim i As Long, ii As Long, iii As Long, n As Long
Dim a, b, e, s, txt As String, temp, r As Range
Application.ScreenUpdating = False
With Sheets(1).Cells(1).CurrentRegion
.Columns(.Columns.Count - 2).Resize(, 3).AutoFill _
Destination:=.Columns(.Columns.Count - 2).Resize(, 6)
With .Cells(1).CurrentRegion.Offset(2).Resize(.Rows.Count - 2)
.Columns(.Columns.Count - 2).Resize(, 3).ClearContents
a = .Value: .ClearContents: .Borders.LineStyle = xlNone
.Font.Bold = False
ReDim b(1 To Rows.Count, 1 To UBound(a, 2))
.Interior.ColorIndex = xlNone
For i = 1 To UBound(a, 1)
If a(i, 1) <> "" Then temp = a(i, 1)
If a(i, 2) <> "TOTAL" Then
n = n + 1
For ii = 1 To UBound(a, 2) - 3
b(n, ii) = a(i, ii)
Next
If dic.exists(temp) Then
txt = Join(Array(a(i, 2), a(i, 3)), "")
If dic(temp).exists(txt) Then
b(n, UBound(a, 2) - 2) = dic(temp)(txt)(3)
b(n, UBound(a, 2) - 1) = dic(temp)(txt)(4)
b(n, UBound(a, 2)) = b(n, UBound(a, 2) - 2) - b(n, UBound(a, 2) - 1)
dic(temp).Remove txt
If dic(temp).Count = 0 Then dic.Remove temp
End If
End If
Else
If dic.exists(temp) Then
If dic(temp).Count Then
For Each e In dic(temp)
n = n + 1
For ii = 0 To 2
b(n, ii + 2) = dic(temp)(e)(ii)
Next
b(n, UBound(b, 2) - 2) = dic(temp)(e)(3)
b(n, UBound(b, 2) - 1) = dic(temp)(e)(4)
b(n, UBound(a, 2)) = b(n, UBound(a, 2) - 2) - b(n, UBound(a, 2) - 1)
dic(temp).Remove e
Next
If dic(temp).Count = 0 Then dic.Remove temp
End If
End If
n = n + 1: b(n, 2) = "TOTAL"
End If
Next
If dic.Count Then
For Each e In dic
n = n + 1: b(n, 1) = e
For Each s In dic(e)
For ii = 0 To 2
b(n, ii + 2) = dic(e)(s)(ii)
Next
b(n, UBound(b, 2) - 2) = dic(e)(s)(3)
b(n, UBound(b, 2) - 1) = dic(e)(s)(4)
b(n, UBound(a, 2)) = b(n, UBound(a, 2) - 2) - b(n, UBound(a, 2) - 1)
n = n + 1
Next
b(n, 2) = "TOTAL"
Next
End If
With .Resize(n)
.Value = b
For Each r In .Columns(3).SpecialCells(2).Areas
If r(r.Count + 1, 0) = "TOTAL" Then
r(r.Count + 1, 3).Resize(, UBound(b, 2) - 4).Formula = _
"=sum(" & r.Offset(, 2).Address(0, 0) & ")"
With r(r.Count + 1, 0).Resize(, UBound(b, 2) - 1)
.Interior.Color = 11573124
.Font.Bold = True
End With
End If
Next
.HorizontalAlignment = xlCenter
.Offset(, 1).Resize(, .Columns.Count - 1).Borders.Weight = 2
.Rows.AutoFit
End With
End With
End With
Application.ScreenUpdating = True
End Sub
Bookmarks