Data extraction from multiple excel files into one spreadsheet

Hello,

I am cludging my through vba and have reused the following code to loop through all the files in a given folder and extract out a specific cell value, in this case "F19" and then put that into a new spreadsheet. It works, but I also need the file name from where the data came to be included in another column, preferably to the left, so the final table looks like:
filename  value
 f1            21
f2              57
etc.


Thanks,

Bruce
Sub extract()
'LOOP THROUGH ALL THE work books and extract a value


Dim xlBook As Excel.Workbook
fname = Dir("C:\Documents and Settings\bird\Desktop\copy of _2011_12_Q2_output\*.xls")
Do While (fname <> "")
Set xlBook = Workbooks.Open("C:\Documents and Settings\bird\Desktop\copy of _2011_12_Q2_output\" & fname)
  
xlBook.Worksheets("F1_AAH_Consolidated").Activate
    Range("f19").Select
  Selection.Copy
    Windows("Book2").Activate
            ActiveCell.PasteSpecial
       ActiveCell.Offset(1, 0).Range("A1").Select
       

xlBook.Save
xlBook.Close
fname = Dir
Loop
Set xlBook = Nothing
End Sub

Open in new window

LVL 1
DiaphanosomaAsked:
Who is Participating?
 
Saqib Husain, SyedEngineerCommented:
Try replacing

            ActiveCell.PasteSpecial

with

            ActiveCell.offset(0,1).PasteSpecial
            ActiveCell.value=xlbook.name
0
 
DiaphanosomaAuthor Commented:
Thanks,

It was actually
ActiveCell.Offset(1, 0).PasteSpecial
            ActiveCell.Offset(0, 1).Value = xlBook.Name
0
 
DiaphanosomaAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for ssaqibh's comment http:/Q_27393417.html#36957462
Assisted answer: 0 points for Diaphanosoma's comment http:/Q_27393417.html#36957514

for the following reason:

Thanks for your quick response.<br /><br />Bruce
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Saqib Husain, SyedEngineerCommented:
That is not congruous with your question.

ActiveCell.Offset(1, 0).PasteSpecial

will post the value in the next row of the first column

and

            ActiveCell.Offset(0, 1).Value = xlBook.Name

will post the filename in the current row and second column.

So the two will be in the wrong column and on different lines.
0
 
DiaphanosomaAuthor Commented:
Don't know what happend with the closing of the question...I hit multiple solutions by accident and tried to give myself zero points. What a mess.

Anyhow, the tweak to the code does what it is supposed to do. The problem I was having was always writing the value to the same cell on each iteration, so I started with offset down 1. Then the next line of code  moves the cell one to the right and assigns the value of the filename as you demonstrated. So data are on the right line.

Thanks,

Bruce
0
 
DiaphanosomaAuthor Commented:
Thanks,

Bruce
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.