Opening workbooks With Application.FileSearch is dropped in Excel 2007
You should provide other alternative for Excel 2007 users as well.
Rest are good collection to go with..
Thank you,
Discussion on:
View:
Show:
I don't have a 2007 alternative -- maybe a reader will supply one.
Here's my take on an 2007-friendly OpenAllWB alternative. This uses Microsoft Scripting Runtime to look for files:
'******************************************************************************
'* Purpose :
'* This procedure opens all Excel files found within a given directory.
'* Input :
'* strFolder, the folder to look for Excel files within
'******************************************************************************
Sub OpenAllWBInFolder(strFolder As String)
On Error GoTo ErrorHandler:
Dim objFSO As Object
Dim objDir As Object
Dim objFile As Object
Dim lngWbCnt As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDir = objFSO.GetFolder(strFolder)
lngWbCnt = Workbooks.Count
'Loop all files in given directory, looking for Excel files
For Each objFile In objDir.Files
If objFile.Type Like "Microsoft Office Excel*Worksheet" Then
Workbooks.Open objFile.Path
End If
Next objFile
'Warn the user if no files was found to open
If lngWbCnt = Workbooks.Count Then
MsgBox "There are no workbooks to open", vbOKOnly + vbInformation, _
"OpenAllWBInFolder"
End If
ExitProc:
On Error Resume Next
Set objFSO = Nothing
Set objDir = Nothing
Set objFile = Nothing
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 5, 76
' 5 = Invalid procedure call or argument
'76 = Path not found
MsgBox "Invalid path specified", vbOKOnly + vbInformation, _
"OpenAllWBInFolder"
Case 429 'Can't create ActiveX
'Microsoft scripting runtime probably not installed
MsgBox "'Microsoft scripting runtime' must be installed in order" _
& vbCr & "for this procedure to work.", _
vbOKOnly + vbInformation, "OpenAllWBInFolder"
Case Else
MsgBox "[" & Err.Source & "]" & vbCrLf & "An unexpected run-time" _
& " error occured '" & CStr(Err.Number) & "':" & vbCrLf _
& vbCrLf & Err.Description, vbExclamation, "OpenAllWBInFolder", _
Err.HelpFile, Err.HelpContext
End Select
Resume ExitProc:
End Sub
'******************************************************************************
'* Purpose :
'* This procedure opens all Excel files found within a given directory.
'* Input :
'* strFolder, the folder to look for Excel files within
'******************************************************************************
Sub OpenAllWBInFolder(strFolder As String)
On Error GoTo ErrorHandler:
Dim objFSO As Object
Dim objDir As Object
Dim objFile As Object
Dim lngWbCnt As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDir = objFSO.GetFolder(strFolder)
lngWbCnt = Workbooks.Count
'Loop all files in given directory, looking for Excel files
For Each objFile In objDir.Files
If objFile.Type Like "Microsoft Office Excel*Worksheet" Then
Workbooks.Open objFile.Path
End If
Next objFile
'Warn the user if no files was found to open
If lngWbCnt = Workbooks.Count Then
MsgBox "There are no workbooks to open", vbOKOnly + vbInformation, _
"OpenAllWBInFolder"
End If
ExitProc:
On Error Resume Next
Set objFSO = Nothing
Set objDir = Nothing
Set objFile = Nothing
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 5, 76
' 5 = Invalid procedure call or argument
'76 = Path not found
MsgBox "Invalid path specified", vbOKOnly + vbInformation, _
"OpenAllWBInFolder"
Case 429 'Can't create ActiveX
'Microsoft scripting runtime probably not installed
MsgBox "'Microsoft scripting runtime' must be installed in order" _
& vbCr & "for this procedure to work.", _
vbOKOnly + vbInformation, "OpenAllWBInFolder"
Case Else
MsgBox "[" & Err.Source & "]" & vbCrLf & "An unexpected run-time" _
& " error occured '" & CStr(Err.Number) & "':" & vbCrLf _
& vbCrLf & Err.Description, vbExclamation, "OpenAllWBInFolder", _
Err.HelpFile, Err.HelpContext
End Select
Resume ExitProc:
End Sub
What if you know the worksheet VBobject, but not it's sheet name? How can you delete the sheet? Say the VBobject is Sheet1, and the sheet name is something else UNKNOWN. You cannot say Sheet1.Delete, that's not allowed. So how can you get the sheet name or sheet object, when all you know is the VB object behind the worksheet?
Method 1: ThisWorkbook.Worksheets(VBobject.Name).Delete
Explanation: VBobject.Name returns the worksheet name.
Except this does not always work! SOMETIMES, VBobject.Name does NOT return the sheet name, it returns the VBobject name!
Method 2: VBobject.Properties(7) ALWAYS returns the name of the worksheet. Yes, 7.
Dim sSheetName as String
sSheetName = VBobject.Properties(7)
Worksheets(sSheetName).Delete
Method 1: ThisWorkbook.Worksheets(VBobject.Name).Delete
Explanation: VBobject.Name returns the worksheet name.
Except this does not always work! SOMETIMES, VBobject.Name does NOT return the sheet name, it returns the VBobject name!
Method 2: VBobject.Properties(7) ALWAYS returns the name of the worksheet. Yes, 7.
Dim sSheetName as String
sSheetName = VBobject.Properties(7)
Worksheets(sSheetName).Delete
- Keyboard Shortcuts:
- Prev
- Next
- Toggle

































