Excel - VBA Question. Need to access data from all excel files in a directory without opening the files

前端 未结 5 910
天涯浪人
天涯浪人 2020-12-04 04:20

So I have a \"master\" excel file that I need to populate with data from excel files in a directory. I just need to access each file and copy one line from the second sheet

相关标签:
5条回答
  • 2020-12-04 04:31

    brettdj and paulsm4 answers are giving much information but I still wanted to add my 2 cents.

    As iDevlop answered in this thread ( Copy data from another Workbook through VBA ), you can also use GetInfoFromClosedFile().

    0 讨论(0)
  • 2020-12-04 04:32

    Some bits from my class-wrapper for Excel:

    Dim wb As Excel.Workbook
    Dim xlApp As Excel.Application
    
    Set xlApp = New Excel.Application
    xlApp.DisplayAlerts = False  ''# prevents dialog boxes
    xlApp.ScreenUpdating = False ''# prevents showing up
    xlApp.EnableEvents = False   ''# prevents all internal events even being fired
    
    ''# start your "reading from the files"-loop here
        Set wb = xlApp.Workbooks.Add(sFilename) '' better than open, because it can read from files that are in use
    
        ''# read the cells you need...
        ''# [....]
    
        wb.Close SaveChanges:=False     ''# clean up workbook
    ''# end your "reading from the files"-loop here
    
    ''# after your're done with all files, properly clean up:
    xlApp.Quit
    Set xlApp = Nothing
    

    Good luck!

    0 讨论(0)
  • 2020-12-04 04:35

    You dont have to open the files (ADO may be an option, as is creating links with code, or using ExecuteExcel4Macro) but typically opening files with code is the most flexible and easiest approach.

    1. Copy a range from a closed workbook (ADO)
    2. ExecuteExcel4Macro
    3. Links method

    But why don't you want to open the files - is this really a hard constraint?

    My code in Macro to loop through all sheets that are placed between two named sheets and copy their data to a consolidated file pulls all data from all sheets in each workbook in a folder together (by opening the files in the background).

    It could easily be tailored to just row X of sheet 2 if you are happy with this process

    0 讨论(0)
  • 2020-12-04 04:38

    I just want to point out: You don't strictly need VBA to get values from a closed workbook. You can use a formula such as:

    ='C:\MyPath\[MyBook.xls]Sheet1'!$A$3
    

    You can implement this approach in VBA as well:

    Dim rngDestinationCell As Range
    Dim rngSourceCell As Range
    Dim xlsPath As String
    Dim xlsFilename As String
    Dim sourceSheetName As String
    
    Set rngDestinationCell = Cells(3,1) ' or Range("A3")
    Set rngSourceCell = Cells(3,1)
    xlsPath = "C:\MyPath"
    xlsFilename = "MyBook.xls"
    sourceSheetName = "Sheet1"
    
    rngDestinationCell.Formula = "=" _
        & "'" & xlsPath & "\[" & xlsFilename & "]" & sourceSheetName & "'!" _
        & rngSourceCell.Address
    

    The other answers present fine solutions as well, perhaps more elegant than this.

    0 讨论(0)
  • 2020-12-04 04:49

    At the start of your macro add

    Application.ScreenUpdating = false
    

    then at the end

    Application.ScreenUpdating = True
    

    and you won't see any files open as the macro performs its function.

    0 讨论(0)
提交回复
热议问题