List files in a folder and capture one cell data from each?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello, hope someone can help, I've got myself into a bit of a pickle with this :)

I have a folder, say, c:\users\test that contains a number of Excel files (*.xlsx). This will vary from month to month but let's say for this question it contains 1.xlsx, 2.xlsx, 3.xlsx (i.e three files).

What I want to do is loop through the folder and list the filename in Sheet1 column A. Then I want to open each file, capture the email address that is held on a tab named "emails" (cell A1) and then close the file.

The email address will go into Sheet1 column B.

My output would look something like this
Code:
1.xlsx donald.trump@whitehouse.gov.us
2.xlsx vladimir.putin@kremlin.ru
3.xlsx bill.gates@microsoft.com

Is this do-able?

Many thanks!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
try


VBA Code:
Sub LoopThroughFiles()

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim Fcount As Integer
Dim StrtRow As Integer
Dim StrtCol As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder("C:\MyWork")
StrtRow = 21
StrtCol = 1
Fcount=0

For Each oFile In oFolder.Files
    If Right(oFile.Name, 4) = "xlsx" Then
        Cells(StrtRow, StrtCol) = oFile.Name
        StrtRow = StrtRow + 1
        Fcount = Fcount + 1
    End If
Next oFile
End Sub
 
Upvote 1
Most things are doable.

Try this.

VBA Code:
Private Sub subGetEmailAddresse()
Dim WbActive As Workbook
Dim intLastRow As Integer
Dim strFile As String
Dim strPath As String
Dim intCounter As Integer

    strPath = "c:\users\test\"
    
    Set WbActive = ActiveWorkbook
    
    Application.ScreenUpdating = False
    
    strFile = Dir(strPath & "*.xlsx")
        
    While strFile <> vbNullString
    
        intCounter = intCounter + 1
    
        Workbooks.Open filename:=strPath & strFile
        
        intLastRow = WbActive.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
        
        WbActive.Sheets("Sheet1").Cells(intLastRow, 1).Value = strFile

        WbActive.Sheets("Sheet1").Cells(intLastRow, 2).Value = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
        
        ActiveWorkbook.Close
    
        strFile = Dir
    
    Wend
    
    Application.ScreenUpdating = True
    
    MsgBox intCounter & " files processed.", vbInformation, "Confirmation"
        
End Sub
 
Upvote 1
Solution
Thanks both, I'll try the solutions out when I'm back in work tomorrow.

I appreciate your time in assisting.
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,849
Members
449,194
Latest member
HellScout

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