Returning range of values from all files in folder

GDunn

Board Regular
Joined
Mar 24, 2009
Messages
51
Hi,

I am trying to return a range of values ("F23:K23") from each sheet named ("Test") from each file in a folder named ("January 2011"), to a seperate worksheet.

Each time the refresh button is pressed the code should look at each file in the specified folder and return the range on each sheet named ("Test") within the files in the folder ("January 2011") to Sheet ("Master") - first file results on return to cells ("A1:F1"), second file results return to cell ("A2:F2") and so on...

No code of any use to offer at this time. Can anyone help?

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Code:
Private Sub ReturnData()

    ' Set reference to library: Tools -> Reference -> Microsoft Scripting Runtime

    Dim fso As New FileSystemObject, aFile As File
    Dim wkb As Workbook, sh As Worksheet, this As Worksheet
    
    Set this = Worksheets("Master")
    
    For Each aFile In fso.GetFolder("C:\January 2011").Files
        If fso.GetExtensionName(aFile.Name) Like "xls*" Then
            Set wkb = Workbooks.Open(aFile.Path)
            Set sh = wkb.Sheets("Test")
            sh.Range("F23:K23").Copy this.Cells(this.Rows.Count, "A").End(xlUp).Offset(1, 0)
            wkb.Close SaveChanges:=False
        End If
    Next

    this.Rows(1).EntireRow.Delete
    
End Sub
 
Upvote 0
Dim fso As New FileSystemObject, aFile As file
is returning Compile Error: User-defined type not defined

+ do I need to do anything re: this comment?
' Set reference to library: Tools -> Reference -> Microsoft Scripting Runtime

Thanks
 
Upvote 0
See comment. And also change path to your folder:
Code:
For Each aFile In fso.GetFolder("[B][COLOR="Red"]C:\January 2011[/COLOR][/B]").Files
 
Upvote 0
Apologies, I do not understand comment

Have changed folder references
 
Last edited:
Upvote 0
Comments begin with apostrophe and Rem keyword.
For example:
Code:
Sub SubWithComments
   ' This is comment. I begin with apostrophe.
   Rem This is comment too. I begin with Rem keyword.
End Sub

So, you should go to Tools -> References and check "Microsoft Scripting Runtime". Good luck! :)
 
Upvote 0
Code:
Private Sub ReturnData()

    Dim fso As New FileSystemObject, aFile As File
    Dim wkb As Workbook, sh As Worksheet, this As Worksheet
    
    ' Set reference to library: Tools -> Reference -> Microsoft Scripting Runtime
    Set this = Worksheets("Master")
    
    For Each aFile In fso.GetFolder("C:\January 2011").Files
        If fso.GetExtensionName(aFile.Name) Like "xls*" Then
            Set wkb = Workbooks.Open(aFile.Path)
            Set sh = wkb.Sheets("Test")
            [B][COLOR="Red"]sh.Range("F23:K23").Copy
            this.Cells(this.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues[/COLOR][/B]
            wkb.Close SaveChanges:=False
        End If
    Next

    this.Rows(1).Delete
    
End Sub
 
Upvote 0
Change this line:

Code:
sh.Range("F23:K23").Copy this.Cells(this.Rows.Count, "A").End(xlUp).Offset(1, 0)

to this:

Code:
sh.Range("F23:K23").Copy 
this.Cells(this.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,334
Members
452,907
Latest member
Roland Deschain

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