View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
Zone[_3_] Zone[_3_] is offline
external usenet poster
 
Posts: 373
Default Looping thru multiple files to produce a consolidated summary by Code

Okay, Celeste, here is my attempt. It's only concerned with the 2 periods
that currently exist, so of course it will need to be tweeked as additional
periods are added. Note that I have changed my original code, including the
function. Let me know how it works for you! James

Sub CreateSummary()
Dim k As Long, Sht As Integer, TableBtm As Long
Dim j As Integer, TableRt As Integer
Dim newDesc As String, ToRow As Long
'check that codes are in Code Table
Worksheets(1).Activate
For Sht = 2 To 3
With Worksheets(Sht)
TableBtm = [a3].End(xlDown).Row
For k = 3 To TableBtm
If FindCode(.Cells(k, "a")) = 0 Then
newDesc = InputBox("Enter description " _
& " for Code " & .Cells(k, "a"))
If newDesc = "" Then
Exit Sub
Else
TableBtm = TableBtm + 1
Cells(TableBtm, "a") = .Cells(k, "a")
Cells(TableBtm, "b") = newDesc
End If
End If
Next k
End With
Next Sht
'sort Code Table
Range("a3:b" & TableBtm).Sort key1:=Range("a3"), _
Order1:=xlAscending, header:=xlNo
'copy Code Table to Summary Table
Worksheets(4).Cells.Clear
TableRt = [a1].End(xlToRight).Column
Range(Cells(1, 1), Cells(TableRt, TableBtm)).Copy _
Destination:=Worksheets(4).[a1]
Worksheets(4).Activate
'set up summary table
Columns(2).AutoFit
[a1] = "Summary Table"
[c2] = "Period 1"
[d2] = "Period 2"
[e2] = "Total"
For k = 3 To TableBtm
For j = 3 To 4
Cells(k, j) = 0
Next j
Cells(k, 5) = "=sum(c" & k & ":d" & k & ")"
Next k
Cells(TableBtm + 2, 5) = "=sum(e3:e" & TableBtm & ")"
'get period information
For Sht = 2 To 3
With Worksheets(Sht)
For j = 3 To .Cells(3, "a").End(xlDown).Row
ToRow = FindCode(.Cells(j, "a"))
Cells(ToRow, Sht + 1) = _
Cells(ToRow, Sht + 1) + .Cells(j, "b")
Next j
End With
Next Sht
End Sub

Function FindCode(myCode) As Long
Dim c As Range
FindCode = 0
Set c = Columns(1).Find(myCode, _
lookat:=xlWhole, LookIn:=xlValues)
If Not c Is Nothing Then FindCode = c.Row
End Function


"u473" wrote in message
ups.com...
Precisions : For my grasping of VBA, I would like to see both versions
Version1 : This cycle would have Period1 and Period2, in the Summary
Table.
each following cycle would add one Period, to a
maximum of 12 Periods.

Version2 : A predefined Summary Table of 12 Periods.

Headers on 2 rows. Data starting in row 3 for all tables.
Thank you again,
Celeste