MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Compilation of data from different files.

Posted by krishna on December 22, 2001 7:43 PM

I wanted to know how can we compile data from different excel file. I have data of financial performance of 50 companies in same format. Now I want to Compile a single row from all the files. For eg. Row 10 gives Profit for last ten years in all the files. I want a file with file name as row heading and all the profit figures. How can I do that?



Posted by Ivan F Moala on December 23, 2001 2:20 AM

This routine will help you get started.
It gets values from a sheet named Sheet1 @
A1 for each file in a specified Dir. You will
have to amend as required......for further help'll probably need a looping routine
here as well BUT I don't know your requirements
for the cell data placements.

Sub GetValue_ViaFormula()
Dim sDir As String
Dim ShtCellLoc As String
Dim DRg As Range
Dim Files
Dim x As Double

'This is the Dir to search in
sDir = "C:\Excelfiles\"
'This is the Location/cell address
ShtCellLoc = "Sheet1'!$A$1"

Files = Dir(sDir & "*.XLS")

'Clear area Column A to place data in
'Change this as required

'speed things up
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

x = 1
On Error GoTo FileError
Do While Len(Files) > 0
Cells(1, x) = Files
Cells(2, x) = "='" & sDir & "[" & Files & "]" & ShtCellLoc
x = x + 1
Files = Dir()
Set DRg = Range(Range("A1:A2"), Range("A1:A2").End(xlToRight)) '.Copy
DRg.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

Set DRg = Nothing

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

MsgBox "Done!"

Exit Sub
MsgBox Err.Number & Chr(13) & _
Err.Description & Chr(13) _
, vbCritical + vbMsgBoxHelpButton, _
"File Error", _
Err.HelpFile, _
End Sub