Listing excel files in a folder

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
Can anybody help a bit I please, I am putting together some code to list the excel files in a folder in column B, open each one in turn copy a value from cell D142 and paste in column C next to the file name.
What this should achieve is the a filename in column B and the description next to it in column C
I’m sure there are lots of issues with this code but at the moment I cannot get it to open the file so I can copy the data
All help is much appreciated.

Code:
Dim i As Long
Dim WB As Workbook
Dim CurrentSheet As Worksheet

Sub GetDescription()

Set ActWork = ActiveWorkbook

Worksheets("Sheet1").Select

myDir = "W:\Sub-Contract\Test\Cost Sheets"
myFile = Dir(myDir & Application.PathSeparator, vbDirectory)

Range("B3:B500").ClearContents
    Do While myFile <> ""
            i = i + 1
                Cells(i, 2).Offset(2, 0) = myFile
                    myFile = Dir
                Workbooks.Open Filename:=myDir
            Sheets("Cost Sheet").Select
        Range("D142").Copy
    wkbk.Activate 'Select Orginal Workbook
myFile.Offset(0, 1).Paste 'Paste description onto cost sheets list next to the file name
Loop

For i = 1 To Range("B65536").End(xlUp).Row

Next

End Sub

I have managed some code to work just listing the file names but cannot move to the next step and open each one as it is listed. I did originally think of listing the files first then going back and opening each one in turn from the list.

Code:
Dim i As Long
Sub List_CostSheets()

' this is listing the cost sheets only
Set ActWork = ActiveWorkbook

Worksheets("Sheet1").Select

'myDir = "W:\1works managers files\Cost sheets"
myDir = "W:\Sub-Contract\Test\Cost Sheets"

myFile = Dir(myDir & Application.PathSeparator & "*.xlsx", vbDirectory)
'Clear Data in Column B
    Range("B3:B500").ClearContents
            Do While myFile <> ""
            i = i + 1
        Cells(i, 2).Offset(2, 0) = myFile
    myFile = Dir
Loop

For i = 1 To Range("B65536").End(xlUp).Row

Next

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try
Code:
Sub List_CostSheets()
   Dim i As Long
   Dim Ws As Worksheet
   Dim Wbk As Workbook
   
   Set Ws = Worksheets("Sheet1")
   
   'myDir = "W:\1works managers files\Cost sheets"
   mydir = "W:\Sub-Contract\Test\Cost Sheets"
   
   myfile = Dir(mydir & Application.PathSeparator & "*.xlsx", vbDirectory)
   'Clear Data in Column B
   Ws.Range("B3:B500").ClearContents
   i = 3
   Do While myfile <> ""
      Ws.Cells(i, 2) = myfile
      Set Wbk = Workbooks.Open(mydir & "\" & myfile)
      Ws.Cells(i, 3).Value = Wbk.Sheets("Cost Sheet").Range("D142").Value
      Wbk.Close False
      myfile = Dir
      i = i + 1
   Loop
End Sub
 
Upvote 0
Thanks Fluff that works great
Some of the excel files have links and I get a message "do I wish to update links"
How can I open the files as read only and not update links
 
Upvote 0
Try
Code:
Set Wbk = Workbooks.Open(mydir & "\" & myfile,False)
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,519
Members
448,968
Latest member
Ajax40

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