Dim dictFolders As New Scripting.Dictionary
Dim dictFiles As New Scripting.Dictionary
Dim dictFileLike As New Scripting.Dictionary
Dim dictFolderLike As New Scripting.Dictionary
Private Sub subVBAAddANewColumnInMultipleWorkbooks()
Dim varFolderKey As Variant
Dim varFileKey As Variant
Dim strPath As String
Dim fso As Object
Dim objFolder As Object
ActiveWorkbook.Save
dictFolders.RemoveAll
dictFolderLike.RemoveAll
dictFiles.RemoveAll
dictFileLike.RemoveAll
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = "C:\Private\Microsoft Office Documents\Excel\Mr Excel\VBA - Add a new column in multiple workbooks\Files"
If Dir(strPath, vbDirectory) = "" Then
MsgBox "Folder does not exist", vbInformation, "Warning!"
Exit Sub
End If
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
Set objFolder = fso.GetFolder(strPath)
dictFolderLike.Add Key:="*", Item:=dictFolderLike.Count + 1
If dictFolderLike.Count = 0 Then
dictFolderLike.Add Key:="*", Item:=dictFolderLike.Count + 1
End If
Call subPopulateFoldersDictionary(objFolder)
Set objFolder = Nothing
Set fso = Nothing
dictFileLike.Add Key:="*.xls", Item:=dictFileLike.Count + 1
dictFileLike.Add Key:="*.xlsx", Item:=dictFileLike.Count + 1
dictFileLike.Add Key:="*.xlsm", Item:=dictFileLike.Count + 1
If dictFileLike.Count = 0 Then
dictFileLike.Add Key:="*", Item:=dictFileLike.Count + 1
End If
For Each varFolderKey In dictFolders.Keys
Call subPopulateFileNamesDictionary(varFolderKey)
Next varFolderKey
Call subPopulateFileNamesDictionary(strPath)
For Each varFileKey In dictFiles.Keys
Call subOpenWorkbookAndInsertColumn(varFileKey)
Next varFileKey
MsgBox "All files have been amended.", vbInformation, "Confirmation"
Application.ScreenUpdating = True
End Sub
Public Sub subPopulateFoldersDictionary(ByVal objFolder As Object)
Dim SubFolder As Object
Dim subfld As Object
Dim strFolder As String
Dim blnInclude As Boolean
Dim varFolderLike As Variant
For Each SubFolder In objFolder.SubFolders
strFolder = SubFolder.Path
If Right(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If
For Each varFolderLike In dictFolderLike.Keys
If strFolder Like varFolderLike Then
dictFolders.Add Key:=strFolder, Item:=dictFolders.Count + 1
blnInclude = True
Exit For
End If
Next varFolderLike
Next SubFolder
If (dictFolderLike.Count = 0) Or (dictFolderLike.Count > 0 And blnInclude) Then
For Each subfld In objFolder.SubFolders
subPopulateFoldersDictionary subfld
Next subfld
End If
End Sub
Public Sub subPopulateFileNamesDictionary(ByVal strFolder As String)
Dim MyFSO As FileSystemObject
Dim MyFile As File
Dim MyFolder As Folder
Dim varFileLike As Variant
Set MyFSO = New Scripting.FileSystemObject
Set MyFolder = MyFSO.GetFolder(strFolder)
For Each MyFile In MyFolder.Files
For Each varFileLike In dictFileLike.Keys
If MyFile Like varFileLike Then
dictFiles.Add Key:=strFolder & MyFile.Name, Item:=dictFiles.Count + 1
End If
Next varFileLike
Next MyFile
End Sub
Public Sub subOpenWorkbookAndInsertColumn(ByVal strWorkbook As String)
Dim i As Integer
Dim Ws As Worksheet
Dim Wb As Workbook
Dim rngCell As Range
Dim rngColumn As Range
Application.Workbooks.Open strWorkbook
Set Wb = ActiveWorkbook
If fncDoesWorksheetExist(Wb, "Daily Report") Then
Set Ws = Wb.Worksheets("Daily Report")
Ws.Unprotect "Password@1"
Ws.Range("G1").EntireColumn.Insert
Set rngCell = Ws.Range("G1")
With rngCell
.Value = "Local / UC"
.EntireColumn.AutoFit
End With
rngCell.Offset(0, -1).Copy
rngCell.Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Set rngColumn = Ws.Range("A1").CurrentRegion.Columns(1)
Set rngColumn = rngColumn.Resize(rngColumn.Rows.Count - 1.1).Offset(1, rngCell.Column - 1)
rngColumn.Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="Local,Up Country"
Ws.Protect "Password@1"
End If
Wb.Close SaveChanges:=True
End Sub
Public Function fncDoesWorksheetExist(Wb As Workbook, strWorksheet As String) As Boolean
Dim Ws As Worksheet
For Each Ws In Wb.Worksheets
If Ws.Name = strWorksheet Then
fncDoesWorksheetExist = True
Exit For
End If
Next Ws
End Function