Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Sub DoSomething(strFile As String)
  4.  
  5.     Dim wb As Workbook
  6.     Dim myPath As String
  7.     Dim myFile As String
  8.     Dim myExtension As String
  9.     Dim FldrPicker As FileDialog
  10.  
  11.     'Optimize Macro Speed
  12.    Application.ScreenUpdating = False
  13.     Application.EnableEvents = False
  14.     Application.Calculation = xlCalculationManual
  15.  
  16.     Set wb = Workbooks.Open(strFile)
  17.  
  18.     'Ensure Workbook has opened before moving on to next line of code
  19.    DoEvents
  20.  
  21.     'Change First Worksheet's Background Fill Blue
  22.    Application.Calculate
  23.     ActiveWorkbook.RefreshAll
  24.     Application.Wait (Now + TimeValue("0:00:10"))
  25.  
  26.     'Save and Close Workbook
  27.    wb.Close SaveChanges:=True
  28.  
  29.  
  30.     'Reset Macro Optimization Settings
  31.    Application.EnableEvents = True
  32.     Application.Calculation = xlCalculationAutomatic
  33.     Application.ScreenUpdating = True
  34.  
  35. End Sub
  36.  
  37.  
  38. Function GetFiles(ByVal Folder As String) As Collection
  39.  
  40.     Dim strFile As String
  41.  
  42.     Set GetFiles = New Collection
  43.     strFile = Dir(Folder & "\*")
  44.  
  45.     Do While strFile <> ""
  46.         GetFiles.Add strFile
  47.         strFile = Dir
  48.     Loop
  49.  
  50. End Function
  51.  
  52. Function GetFolders(ByVal Folder As String) As Collection
  53.  
  54.     Dim strFile As String
  55.     Set GetFolders = New Collection
  56.  
  57.     strFile = Dir(Folder & "\*", vbDirectory)
  58.  
  59.     Do While strFile <> ""
  60.         If GetAttr(Folder & "\" & strFile) And vbDirectory Then GetFolders.Add strFile
  61.         strFile = Dir
  62.     Loop
  63.  
  64. End Function
  65.  
  66. Sub LoopThroughSubfoldersAsWell()
  67.  
  68.     Dim colFoFi As Collection
  69.     Dim varEl01 As Variant
  70.     Dim varEl02 As Variant
  71.     Dim varEl03 As Variant
  72.     Dim strLine As String: strLine = "--------------------------"
  73.  
  74.     Dim strAddress As String: strAddress = "C:\Users\UserName\Desktop\Testing01\"
  75.  
  76.     Debug.Print strAddress
  77.     Set colFoFi = GetFiles(strAddress)
  78.  
  79.     For Each varEl01 In colFoFi
  80.         Debug.Print varEl01
  81.     Next varEl01
  82.     Debug.Print strLine
  83.  
  84.     Set colFoFi = GetFolders(strAddress)
  85.     For Each varEl01 In colFoFi
  86.         If Len(varEl01) > 2 Then  'to avoid some hidden stuff
  87.  
  88.             Set varEl02 = GetFiles(strAddress & varEl01)
  89.             Debug.Print (strAddress & varEl01)
  90.  
  91.             For Each varEl03 In varEl02
  92.                 Debug.Print varEl03
  93.                 DoSomething (varEl03)
  94.             Next varEl03
  95.  
  96.             Debug.Print strLine
  97.  
  98.         End If
  99.     Next varEl01
  100.  
  101. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement