macro to open files not working in office 2010

surfcarrot

New Member
Joined
May 9, 2008
Messages
4
Hi, I've just upgraded from office 2007 to 2010. The following macro to open any excel files within a particular folder no longer works. I do not get any error msgs; it just looks like it doesn't find anything! Any help appreciated. Code is as follows...

Sub OpenStockSheets()
Dim i As Integer
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
.LookIn = "Z:\Work - Craig\- Masters\Stock sheets"
.FileType = msoFileTypeExcelWorkbooks

If .Execute > 0 Then 'Workbooks in folder
For i = 1 To .FoundFiles.Count 'Loop through all.
'Open Workbook x and Set a Workbook variable to it
Set wbResults = Workbooks.Open(.FoundFiles(i))
Next i
End If
End With

On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
 

Some videos you may like

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
Try...

Code:
[font=Verdana][color=darkblue]Sub[/color] OpenStockSheets()

    [color=darkblue]Dim[/color] wbResults [color=darkblue]As[/color] Workbook
    [color=darkblue]Dim[/color] wbCodeBook [color=darkblue]As[/color] Workbook
    [color=darkblue]Dim[/color] strPath [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] strFile [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Integer[/color]
     
    [color=darkblue]With[/color] Application
        .ScreenUpdating = [color=darkblue]False[/color]
        .DisplayAlerts = [color=darkblue]False[/color]
        .EnableEvents = [color=darkblue]False[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
     
    [color=darkblue]Set[/color] wbCodeBook = ThisWorkbook
    
    [color=green]'Define the path to your folder[/color]
    strPath = "Z:\Work - Craig\- Masters\Stock sheets\"
    
    [color=green]'Ensure that the path ends in a backslash[/color]
    [color=darkblue]If[/color] Right(strPath, 1) <> "\" [color=darkblue]Then[/color] strPath = strPath & "\"
    
    [color=green]'Call the first file within the folder (change the file extension, accordingly)[/color]
    strFile = Dir(strPath & "*.xlsx")
    
    [color=green]'Loop through each file within the folder[/color]
    [color=darkblue]Do[/color] [color=darkblue]While[/color] Len(strFile) > 0
        [color=green]'Open the current file[/color]
        [color=darkblue]Set[/color] wbResults = Workbooks.Open(strPath & strFile)
        [color=green]'Call the next file within the folder[/color]
        strFile = Dir
    [color=darkblue]Loop[/color]
    
    [color=darkblue]With[/color] Application
        .ScreenUpdating = [color=darkblue]True[/color]
        .DisplayAlerts = [color=darkblue]True[/color]
        .EnableEvents = [color=darkblue]True[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,122,491
Messages
5,596,462
Members
414,069
Latest member
StudExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top