VBA - changing where files are drawn from

Dave Coram

New Member
Joined
Apr 18, 2016
Messages
21
Have found the following code on the 'Net and tweaked a little (with some help) to retrieve data from a number of workbooks in a folder, and then present it onto a new spreadsheet.

Public Sub BattingRatings()
On Error GoTo exitloop
Path = "c:\Users\Dave\Desktop\IPL\"
NextFile = Dir(Path & "*.xls")

' open book
Workbooks.Open Filename:=Path & NextFile
' Copy/Paste Data from book
Call RetrieveData
' Close book
ActiveWorkbook.Close savechanges:=False

Do While NextFile <> "" ' Start the loop.
NextFile = Dir
If NextFile = "" Then Exit Sub

' open book
Workbooks.Open Filename:=Path & NextFile
' Copy/Paste Data from book
Call RetrieveData
' Close book
ActiveWorkbook.Close savechanges:=False

Loop
exitloop:
End Sub
Private Sub RetrieveData()
SourceSh = "Ratings"
TargetSh = "Sheet1"

With ThisWorkbook.Sheets(TargetSh)
NxtEmptyRw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
ActiveWorkbook.Sheets(SourceSh).Range("A6:A16,AG6:AG16,AH6:AH16,N6:N16").Copy
.Cells(NxtEmptyRw, 1).PasteSpecial xlPasteValues
End With

With ThisWorkbook.Sheets(TargetSh)
NxtEmptyRw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
ActiveWorkbook.Sheets(SourceSh).Range("A23:A33,AG23:AG33,AH23:AH33,N23:N33").Copy
.Cells(NxtEmptyRw, 1).PasteSpecial xlPasteValues
End With
End Sub

Please could someone point me in the right direction as to how I would tweak it to allow for the selection of workbooks to be retrieved from, rather than retrieving from all workbooks - would like to update it as we go through the tournament, rather than having to wait to the end!

Have found stuff about GetOpenFilename, and understand the logic, but can't work out how to link that to what I have.

Thanks.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,216,219
Messages
6,129,577
Members
449,519
Latest member
Rory Calhoun

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