Option Explicit
Dim mbChecked As Boolean
Sub UpdateSomeProjectWorkbooks()
Const cDocPropName As String = "ProjectName"
Const cDocPropVal As String = "Data"
Dim lUbk As Long
Dim szFolderPath As String
Dim objFolder As Object
Dim szbkName As String
Dim wbk As Workbook
Dim i As Long
Set objFolder = CreateObject("Shell.Application"). _
BrowseForFolder _
(0, "Select the folder containing workbooks to update", 0, _
Empty)
If Not objFolder Is Nothing Then
On Error GoTo ErrExit
If Len(objFolder.items.Item.Path) > 3 Then
szFolderPath = objFolder.items.Item.Path & Application.PathSeparator
Else
szFolderPath = objFolder.items.Item.Path
End If
On Error GoTo 0
Else
Exit Sub
End If
If szFolderPath <> ThisWorkbook.Path & "\" Then
With Application.FileSearch
.NewSearch
.LookIn = szFolderPath
.SearchSubFolders = False
.Filename = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
With Application
.ScreenUpdating = False
.EnableEvents = False
If Val(.Version) >= 9 Then
.ShowWindowsInTaskbar = False
End If
End With
If .Execute() > 0 Then
lUbk = 0
For i = 1 To .FoundFiles.Count
Set wbk = Application.Workbooks.Open(.FoundFiles(i))
Application.StatusBar = "Currently Checking: " & wbk.Name
On Error Resume Next
If wbk.CustomDocumentProperties(cDocPropName).Value <> cDocPropVal Then
Err.Clear
wbk.Close False
Else
mbChecked = False
Call ExampleUpdateCode
If mbChecked Then
lUbk = lUbk + 1
szbkName = szbkName & vbNewLine & wbk.Name
End If
With wbk
.Save
.Close
End With
End If
Next i
Else
MsgBox "There were no files found.", 16
Exit Sub
End If
End With
Set wbk = Nothing
Else
MsgBox "This master workbook cannot be in the same folder as the one being searched", 64
Exit Sub
End If
ErrExit:
With Application
.ScreenUpdating = True
.EnableEvents = True
.StatusBar = Empty
If Val(.Version) >= 9 Then
.ShowWindowsInTaskbar = True
End If
End With
If lUbk > 0 Then
MsgBox "Updated " & lUbk & " project workbooks" & vbNewLine & szbkName, 64
Else
MsgBox "No workbooks were updated", 64
End If
End Sub
Private Sub ExampleUpdateCode()
On Error Resume Next
With ActiveWorkbook
.Sheets("2005").Select
End With
If Err.Number <> 0 Then
ThisWorkbook.Sheets("2005").Copy After:=ActiveWorkbook.Sheets(Worksheets.Count)
mbChecked = True
Else
Exit Sub
mbChecked = False
End If
End Sub
|