Advertisement
AlanElston

Loop through closed workbooks without opening them

Nov 4th, 2018
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Sub SUMfromD14inClsdWkBksInFolder() ' Loop through closed workbooks without opening them '    http://www.eileenslounge.com/viewtopic.php?f=30&t=31150&p=241152#p241152
  3. ' Use Dir function with wildcards in full path and name search string to find file names you want
  4. Dim FileName As String:
  5.  Let FileName = Dir("C:\Users\Elston\Desktop\YassersFolder\*record*", vbNormal) ' The Dir function uased the first time here, it will find the first file with "record" in its file name in the folder , "YassersFolder". If it does not find one,  it will return "". If it finds one, then variable FileName will be given its name, ( just the name, not the entire file path and name)
  6. 'Do do Looping while you find the file names you want =========
  7.    Do While Not FileName = "" ' Dir Function will return "" if it finds no new File names of the ones looking for. If it does find a File name, then use that filename in the closed workbook reference which you put in a spare cell, for example, A1
  8.     Let ThisWorkbook.Worksheets.Item(1).Range("A1").Value = "=" & "'" & "C:\Users\Elston\Desktop\YassersFolder\" & "[" & FileName & "]Tabelle1'!$D$14"
  9.     Dim SomeTotal As Double ' A variable to hold the Sum total so far
  10.     Let SomeTotal = SomeTotal + ThisWorkbook.Worksheets.Item(1).Range("A1").Value
  11.      Let FileName = Dir ' an unqualified Dir will look again using the last search criteria, so the first time this line is used, Dir Function  will try to find a second file with the string part "record" in its file name
  12.    Loop '  do while you find the file names you want ==========
  13. Let ThisWorkbook.Worksheets.Item(1).Range("A10").Value = SomeTotal
  14. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement