Loop through folder and get values from spreadsheet

Hi Experts,

I need a macro that can loop through a folder and open each excel file and record the name of the excel workbook in column A, the value in cell c4 in Column B, The value In cell I9 in Column C and the value in H9 in Column D.  I would want to record this for each file in the selected directory.

Thanks,
Montrof
LVL 1
montrofAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
I believe this is what you are looking for..

In this code i have made an assumption that from the file from which you are running this macro you have sheet1 and in that you want to populate the data...

Sub getnames()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

    Dim wb As Workbook, ws As Worksheet
    Dim wb1 As Workbook, I As Long
    Dim xpath As String, fname As String

    Set wb = ActiveWorkbook
    Set ws = Sheets("Sheet1")

    xpath = "C:\Users\Montrof\Desktop\New folder"         '<--Your folder path here

    fname = Dir(xpath & "\" & "*.xl??")

    I = 1
    Do While fname <> ""
        Set wb1 = Workbooks.Open(xpath & "\" & fname)
        ws.Cells(I, "A").Value = Left(fname, InStr(1, fname, ".", vbTextCompare) - 1)
        ws.Cells(I, "B").Value = Range("C4").Value
        ws.Cells(I, "C").Value = Range("I9").Value
        ws.Cells(I, "D").Value = Range("H9").Value

        I = I + 1

        wb1.Close (False)
        fname = Dir()
    Loop
    
   
End Sub

Open in new window


Saurabh...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ramesh VCommented:
Hi Try the below:

Sub Loop_A_Folder()
Dim wb As Workbook
Dim ws As Worksheet
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
        .Title = "Select A Target Folder"
        .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With
NextCode:
    myPath = myPath
    If myPath = "" Then GoTo ResetSettings
    myExtension = "*.xls*"
    myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
    Do While myFile <> ""
        Set wb = Workbooks.Open(Filename:=myPath & myFile)
        Debug.Print myFile
        wb.Close SaveChanges:=True
        myFile = Dir
    Loop
End Sub
Ramesh VCommented:
This will write the file name to immediate window.
Make changes according to your requirement that's not the final code.
montrofAuthor Commented:
Thanks this works great
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.