VBA to extract Rows from several sheets

irishr_ram

New Member
Joined
Jul 16, 2013
Messages
10
Hi All,

I have a requirement to extract several rows from different spreadsheet files.

I can with the below code extract a row from each as it just loops through the files but cannot figure how to get it to loop through each spreadsheet on the required tab until the end of the rows and then move onto the next file. Also once I get it to do that I want to be able to incorporate that each set of rows from each sheet is pasted below each other combined.

Any help appreciated

Code:
rowTarget = 3
rowSource = 5


Set wsCostCentreView = Sheets("Cost Centre View")


Folderpath = "C:\Users\fxd38\Desktop\Data"
Filename = Dir(Folderpath & "*.xls*")


Do While Filename <> ""


       'open the source file and set the source worksheet
      Set wbSource = Workbooks.Open(Folderpath & Filename, ReadOnly:=True, UpdateLinks:=False)
      
      On Error Resume Next
      Set wsForecastHourly = wbSource.Worksheets("Forecast - Hourly")
      Set wsForecastAC = wbSource.Worksheets("Forecast - Additional Costs")
      Set wsInfo = wbSource.Worksheets("Info")
      
      Application.StatusBar = "Updating" & " " & wsInfo.Range("B2").Value & " " & "Financials"
      
      'wsForecastHourly.Range("B5", Range("B" & row.Count).End(xlUp)).Copy
      'wsCostCentreView.Range("C3").End(xlUp)(2).PasteSpecial xlPasteValues
      
      
   wsCostCentreView.Range("C" & rowTarget).Value = wsForecastHourly.Range("B" & rowSource).Value
   wsCostCentreView.Range("D" & rowTarget).Value = wsForecastHourly.Range("C" & rowSource).Value
   'wsCostCentreView.Range("E" & rowTarget).Value = wsForecastAC.Range("A4").Value
   wsCostCentreView.Range("F" & rowTarget).Value = wsForecastHourly.Range("A" & rowSource).Value
   'wsCostCentreView.Range("G" & rowTarget).Value = wsForecastAC.Range("B4").Value
   wsCostCentreView.Range("H" & rowTarget).Value = wsForecastHourly.Range("D" & rowSource).Value


'ActiveCell.Offset(1, 0).Select


'row = row + 1


'close the source workbook, increment the output row and get the next file
wbSource.Close SaveChanges:=False
rowTarget = rowTarget + 1
rowSource = rowSource + 1
Filename = Dir()


Loop
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi , sorry I can't prvide help but perhaps you can help me as I need a similar code as you've already got! I need to extract data from 30 plus difference files if the value in column B of the source files is the same as the one I require. Is this what your code does at present please? thanks,
 
Upvote 0
Hi , sorry I can't prvide help but perhaps you can help me as I need a similar code as you've already got! I need to extract data from 30 plus difference files if the value in column B of the source files is the same as the one I require. Is this what your code does at present please? thanks,

No all my code does above is access all xls* files in a specific folder and extract the column I have specified. You would want something like this but then with some sort of lookup
 
Upvote 0
Still looking for some help on this chaps.

I have got a bit further in that I have inserted some code which now lets me pull all the data back from non blank rows however when I get it to loop through 2 workbooks, the data from the 2nd workbook just overwrites the 1st set of data.

Any help appreciated.

Code below

rowTarget = 3
rowSource = 6


Set wsCostCentreView = Sheets("Cost Centre View")


Folderpath = "H:\Budget Tracker\Data"
Filename = Dir(Folderpath & "*.xls*")


Do Until Filename = ""


'open the source file and set the source worksheet
Set wbSource = Workbooks.Open(Folderpath & Filename, ReadOnly:=True, UpdateLinks:=False)

On Error Resume Next
Set wsForecastHourly = wbSource.Worksheets("Forecast - Hourly")
Set wsForecastAC = wbSource.Worksheets("Forecast - Additional Costs")
Set wsInfo = wbSource.Worksheets("Info")

Application.StatusBar = "Updating" & " " & wsInfo.Range("B2").Value & " " & "Financials"

'wsForecastHourly.Range("B5", Range("B" & row.Count).End(xlUp)).Copy
'wsCostCentreView.Range("C3").End(xlUp)(2).PasteSpecial xlPasteValues

'NumRows = wsForecastHourly.Range("A6", Range("A6").End(xlDown)).rows.Count
Rows = wsForecastHourly.Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count

For x = 1 To Rows
wsCostCentreView.Range("I" & rowTarget).Value = rowTarget
wsCostCentreView.Range("C" & rowTarget).Value = wsForecastHourly.Range("B" & rowSource).Value
wsCostCentreView.Range("D" & rowTarget).Value = wsForecastHourly.Range("C" & rowSource).Value
'wsCostCentreView.Range("E" & rowTarget).Value = wsForecastAC.Range("A4").Value
wsCostCentreView.Range("F" & rowTarget).Value = wsForecastHourly.Range("A" & rowSource).Value
'wsCostCentreView.Range("G" & rowTarget).Value = wsForecastAC.Range("B4").Value
wsCostCentreView.Range("H" & rowTarget).Value = wsForecastHourly.Range("D" & rowSource).Value
ActiveCell.Offset(1, 0).Select




rowTarget = rowTarget + 1
rowSource = rowSource + 1
Filename = Dir()


Next




'close the source workbook, increment the output row and get the next file
wbSource.Close SaveChanges:=False


'row = row + 1


Loop
 
Upvote 0

Forum statistics

Threads
1,224,852
Messages
6,181,406
Members
453,036
Latest member
Koyaanisqatsi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top