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

pull data from files without open them

0

Hello

is  there any  way  to  make  this  code  without  open  files   when  bring  data ?

tha  all files  are  exised  in  one  folder .

Sub CopyRange()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, wb As Workbook, lRow As Long
    Set desWS = ThisWorkbook.Sheets("Sheet1")
    For Each wb In Workbooks
        If wb.Name <> ThisWorkbook.Name Then
            With wb.Sheets("DATA")
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                .Range("A2:E" & lRow).Copy
                desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            End With
        End If
    Next wb
 Application.ScreenUpdating = True
End Sub
Answer
Discuss

Discussion

This post is asking the same thing as the post by Malkal at 2:19am today.
WillieD24 (rep: 557) Dec 13, '22 at 2:11 pm
 you  misread what I  want  !
the  code  works  when  the  all  of  files  are  open .
what  I  want  pull data  without  open  any  file   .
Halk (rep: 30) Dec 13, '22 at 2:58 pm
Add to Discussion

Answers

0
Selected Answer

Halk

As Willie said, the method from my recent answer to Maklal can be modified and combined with your existing code.

Try this (with changes and comments in bold) but define <<your path>> before starting, noting the comment above that line:

Sub CopyRangeFromSetFolder()

Dim desWS As Worksheet, wb As Workbook, lRow As Long
Dim wbNm As String, Fld As String

Application.ScreenUpdating = False

Set desWS = ThisWorkbook.Sheets("Sheet1")
' define path to set folder, ending in \
Fld = "<<your path>>\"

'get first file with wildcard match
    wbNm = Dir(Fld & "*.xls*", vbNormal)
    ' loop while there's another matching file
    Do While wbNm <> ""
       ' Check it's not this workbook
        If wbNm <> ThisWorkbook.Name Then
            With GetObject(Fld & wbNm)
                With .Sheets("DATA")
                    lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                    .Range("A2:E" & lRow).Copy
                    desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                End With
                ' close the file without saving
                .Close False
            End With
        End If

        ' get next matching file
        wbNm = Dir()
    Loop

Application.ScreenUpdating = True
End Sub

You'll see it actually opens then closes each file- you don't have to open them first.

Hope this makes sense and works for you.

Discuss

Discussion

thanks John
  two  things  please:
  1- about  the  path , based  on  the  orginal  code  just  works  the  code  where  the  files are  in  the  folder  without  writing  the  path  because  sometimes move  these files  from PC to  another , then  I must  change  the  path . I  don't want this  way .
2-  I  no  know  why  shows  error  subscript  out  of  range  in  this  line 
With .Sheets("DATA")
despite of the  code  brings  all of  data from the  files  without  any  missed  data and  the  sheet name is  DATA   for  all of  files  except  the  file  where  the  code run  from  it .
Halk (rep: 30) Dec 15, '22 at 4:34 am
Hi Halk

1. Sorry but I don't understand this point (and it's extra to your question)

2. This is probably because you have this file in the same folder as the the .DATA files (and it does not have a DATA sheet). Accordingly I modified my Answer code  so that if ignores this main macro file, extract below (additions in bold):
    Do While wbNm <> ""
       ' Check it's not this workbook
        If wbNm <> ThisWorkbook.Name Then
            With GetObject(Fld & wbNm)
                With .Sheets("DATA")
                    lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                    .Range("A2:E" & lRow).Copy
                    desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                End With
                ' close the file without saving
                .Close False
            End With
        End If
        ' get next matching file
        wbNm = Dir()
    Loop
John_Ru (rep: 6152) Dec 15, '22 at 5:28 am
1. Sorry but I don't understand this point (and it's extra to your question)
in  reality  there  is  no  extra question .
based  on  the  code will loop  throught the  files in the  same  folder without  any  specify  the  directory  as  suggetst  in your  code  .
what  I  meant   like  this 
Fld = ThisWorkbook.Path & "\"

this  idea got  from  the internet 
now  all of  things  are  good 
thanks  very  much  John .
Halk (rep: 30) Dec 15, '22 at 7:11 am
Understood about using ThisWorkbook.Path but I couldn't guess you needed that from your question! Thanks for selecting my Answer,.Halk
John_Ru (rep: 6152) Dec 15, '22 at 7:21 am
Add to Discussion
0

Hello Halk,

Yes, I did understand what you wanted to achieve - copy data without needing the file(s) to be open. I haved tweaked John_Ru's code that he provided for Malkal to work with your situation. It requires the user to select the folder to search. The files in the folder are only "opened in the vba code - not needing files to be fully opened as your method does. You will need to adjust some range references to suit your case.

Sub LoopFolder_2()

Dim LRow As Long
Dim OUTrng As Range
Dim wbNm As String

' get last row number in column B  --  adjust to column number per your situation
LRow = 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
    '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)

            'Set OUTrng = .Sheets(5).Range("g2:i1000")
            Set OUTrng = .Sheets(1).Range("A2:B3")   ' this range was used just for testing purpose
            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 ( in vba code ) without saving
            .Close 0
             Application.DisplayAlerts = True
            ' paste to bottom of row B but don't use helper column  --  adjust to column number per your situation
            ThisWorkbook.Sheets("Sheet1").Cells(LRow + 1, 2).PasteSpecial
        End With

        ' update last row number after data has been pasted -- change 3 to a column number without blanks
        With ThisWorkbook.Sheets("Sheet1")
            LRow = Range("A:M").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row '  --  adjust Range per your situation
        End With

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

End Sub
'

Hopefully this is what you were looking for. Testing file is attached.

Thanks to John for doing the hard work.   ;-)

Cheers

Discuss

Discussion

thanks Willie,
as  I said  you  misread  !
your  answering  depends on select  the  folder , but  in  my  case  all  of  the  files are  existed  in  the  same folder , so  no  need  to  search for  the  folder  by  selection as  in your  suggestion to  pulld  data  and  no  need  to  know  what's  the  path . so I  want  using  as  in  orginal  code   just  keep  the  files are  closed . I  hope  to  my  idea  is  clear.
Halk (rep: 30) Dec 14, '22 at 2:53 am
Hi Halk,

I have searched many, many sites and cannot find any way to copy data without "opening" the workbook to some extent. (There are many methods from simple to complex)
John's solution below might be your best option. It opens a file, copies data, then closes the file without saving. Only one file is "open" at a time.

Cheers   :-)
WillieD24 (rep: 557) Dec 14, '22 at 9:14 pm
thanks willie for  your trying !
Halk (rep: 30) Dec 15, '22 at 2:19 pm
Add to Discussion


Answer the Question

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