Hi,
I need to adapt the following code so that it only opens the workbook based on the condition in bold. However, the code won't run when I remove the .Open after setting the workbook
Can anyone help?
Regards
Sub RunCodeOnAllXLSFiles()
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
Set wbCodeBook = ThisWorkbook
With Application.FileSearch
.NewSearch
'Change path to suit
.LookIn = "C:\Documents and Settings\txc57597\Desktop\CEM 2011\CEM Raw Data\Monthy Data"
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then 'Workbooks in folder
For lCount = 1 To .FoundFiles.Count 'Loop through all
'Open Workbook x and Set a Workbook variable to it
Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
If Format(Now(), "mmm") <> "wbResults '[NOx PI Data]B3'.xls" Then
wbResults.Activate
wbResults.Close SaveChanges:=False
End If
Next lCount
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
I need to adapt the following code so that it only opens the workbook based on the condition in bold. However, the code won't run when I remove the .Open after setting the workbook
Can anyone help?
Regards
Sub RunCodeOnAllXLSFiles()
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
Set wbCodeBook = ThisWorkbook
With Application.FileSearch
.NewSearch
'Change path to suit
.LookIn = "C:\Documents and Settings\txc57597\Desktop\CEM 2011\CEM Raw Data\Monthy Data"
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then 'Workbooks in folder
For lCount = 1 To .FoundFiles.Count 'Loop through all
'Open Workbook x and Set a Workbook variable to it
Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
If Format(Now(), "mmm") <> "wbResults '[NOx PI Data]B3'.xls" Then
wbResults.Activate
wbResults.Close SaveChanges:=False
End If
Next lCount
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub