Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Data extraction from multiple excel files into one spreadsheet

Posted on 2011-10-12
7
Medium Priority
?
210 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Diaphanosoma
  • 4
  • 2
6 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 2000 total points
ID: 36957462
Try replacing

            ActiveCell.PasteSpecial

with

            ActiveCell.offset(0,1).PasteSpecial
            ActiveCell.value=xlbook.name
0
 
LVL 1

Author Comment

by:Diaphanosoma
ID: 36957514
Thanks,

It was actually
ActiveCell.Offset(1, 0).PasteSpecial
            ActiveCell.Offset(0, 1).Value = xlBook.Name
0
 
LVL 1

Author Comment

by:Diaphanosoma
ID: 36957575
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 36957576
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
 
LVL 1

Author Comment

by:Diaphanosoma
ID: 36957696
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
 
LVL 1

Author Closing Comment

by:Diaphanosoma
ID: 36968055
Thanks,

Bruce
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question