Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

select folder istead of select each file to pull data

0

Hi

I  have  question , is  there any  properties  to  select  folder  to  pull data from  multiple  files  for  the  same  sheet and  the  same  range  instead  of  using  select  file   individually as  the  current code  does it?

Sub get_data1()
 Dim lRw As Long
 Dim OUTrng As Range

 ' get last row number in column B
 lRw = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row

 With Application.FileDialog(msoFileDialogFilePicker)
   .Show
      With GetObject(.SelectedItems(1))
        Set OUTrng = .Sheets(5).Range("g2:i1000")
        With OUTrng
            Set OUTrng = OUTrng.Resize(.Rows.Count, .Columns.Count + 1)
        End With
        Application.DisplayAlerts = False
        With OUTrng
            .Resize(.Rows.Count, .Columns.Count - 1).SpecialCells(xlCellTypeVisible).Copy
        End With
        ' close file without saving
        .Close 0
         Application.DisplayAlerts = True
        ' paste to bottom of row B but don't use helper column
        ThisWorkbook.Sheets("sheet1").Cells(lRw + 1, 2).PasteSpecial
      End With
End With

I  hope  my  question  is clear.

Answer
Discuss

Answers

0
Selected Answer

Hi Maklal

If you want to pick individual files in a chosen folder, you can the AllowMultiSelect property of FileDialog. The Microsoft guidance on that is here: FileDialog.AllowMultiSelect property (Office)

To get all files in a folder, you can use the Folder picker instead the use VBA's DIR function to pick the first Excel file (with the chosen folder path plus a wildcard string), process that then use Dir() to get VBA to look for the next matching file in that directory.

In the modified code below, I've made changes in bold and added comments so you see what's happening. Note that I've just searched for .xls* files since you're looking for the first sheet of each in the existing code like your file (and it will fail is there is no such sheet) even though your code above looks for the 5th sheet via Set OUTrng = .Sheets(5:

Sub LoopFolder()

Dim lRw As Long
Dim OUTrng As Range
Dim wbNm As String


'get user to pick a folder
With Application.FileDialog(msoFileDialogFolderPicker)
   ' If okay was pressed
   If .Show = -1 Then
    
    'get first file with wildcard match
    wbNm = Dir(.SelectedItems(1) & "\*.xls*", vbNormal)
    ' loop while there's another matching file
    Do While wbNm <> ""
        With GetObject(.SelectedItems(1) & "\" & wbNm)
            ' get last row number in column B
            lRw = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row
            Set OUTrng = .Sheets(1).Range("g2:i1000")
            With OUTrng
                Set OUTrng = OUTrng.Resize(.Rows.Count, .Columns.Count + 1)
            End With
            Application.DisplayAlerts = False
            With OUTrng
                .Resize(.Rows.Count, .Columns.Count - 1).SpecialCells(xlCellTypeVisible).Copy
            End With
            ' close file without saving
            .Close 0
             Application.DisplayAlerts = True
            ' paste to bottom of row B but don't use helper column
            ThisWorkbook.Sheets("sheet1").Cells(lRw + 1, 2).PasteSpecial
        End With

        ' get next matching file
        wbNm = Dir()
    Loop
   End If
End With

Hope this works for you.

Discuss

Discussion

Hi John ,
thanks  for  that , but  it  seems  to  work with just  one  file . I  tested  for  two  files   it  just  brings  from one  file  and  ignore  the  second  file , and  I  try  keeping  in  the  folder  the  second  file  and  delete the  first  file  to  see  if  there  is  problem  in sheet name  or range,  despite of  everythings  are  matched  and  the  code  pull data from  the  second  file  without  any problem , so  the  code  just  brings data from  one  file.
Malkal (rep: 22) Dec 14, '22 at 4:06 am
Hi Malkal

Are both files *.xlsx or .xlsm (to match the wildcard pattern)?
John_Ru (rep: 6152) Dec 14, '22 at 4:16 am
Try this reduced version (which will debug.print each matching file name plus the  name of its first sheet):

Sub Loop()
 
Dim lRw As Long
Dim OUTrng As Range
Dim wbNm As String
 
' get last row number in column B
lRw = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row
 
'get user to pick a folder
With Application.FileDialog(msoFileDialogFolderPicker)
   ' If okay was pressed
   If .Show = -1 Then
    Application.ScreenUpdating = False
    'get first file with wildcard match
    wbNm = Dir(.SelectedItems(1) & "\*.xls*", vbNormal)
    ' loop while there's another matching file
    Do While wbNm <> ""
        With GetObject(.SelectedItems(1) & "\" & wbNm)
            ' print filename and name of first sheet
            Debug.Print wbNm & ": " & .Sheets(1).Name
            .Close 0
        End With
        
        ' get next matching file
        wbNm = Dir()
    Loop
    Application.ScreenUpdating = False
   End If
End With
 
End Sub
John_Ru (rep: 6152) Dec 14, '22 at 4:22 am
Are both files *.xlsx or .xlsm (to match the wildcard pattern)?
yes  they're

it gives this
MAIN.xlsx: SOURCE
ENTERING.xlsm: SOURCE
Malkal (rep: 22) Dec 14, '22 at 4:56 am
If there are just 2 Excel files in your folder, that shows that my basic folder looping code works (and largely answers your question) . The problem lies in the existing code. If you can't sort it by stepping through using F8 then please attach your macro file and  2 sample files to your question and I will try if I get time.
John_Ru (rep: 6152) Dec 14, '22 at 7:27 am
ok I  uploaded  the  files.
Malkal (rep: 22) Dec 14, '22 at 8:07 am
Malkal   Please see my revised answer- the calculation of lRw needs to be inside the Do While loop (otherwise it overwrites the previous import so you see results from only the last file).

Remember that when you change to.Sheets(5)., you will need each file to have that sheet or it will fail
 
John_Ru (rep: 6152) Dec 14, '22 at 9:59 am
Hi John,
about  change Sheets(5)  is  very understood .
thanks  for  your answering.
Malkal (rep: 22) Dec 14, '22 at 11:59 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login