VBA excel 2010 script not working

Fyr

Active Member
Joined
Jan 20, 2009
Messages
375
I know some stuff no longer works in newer versions of excel.
I'm having trouble trying to figure out how to get this script to work again.

This script opens all .xls files in a specified directory.
Code:
Sub new2010find()
    'MyDir = ActiveWorkbook.Path ' current path"
    strPath = mydir & "P:\CPD\Stats\data" ' files subdir
    With Application.FileSearch
         .NewSearch
        .LookIn = strPath
        .SearchSubFolders = False
        .Filename = ".xls"
        If .Execute > 0 Then
            For Each vaFileName In .FoundFiles
                ProgressBar1.Value = cas
                Workbooks.Open vaFileName, , True
                cas = cas + 1
            Next
        End If
    End With
 
End Sub

Any ideas how to modify it so it works again?
Thanks for taking a look!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try

Code:
Sub new2010find()
    'MyDir = ActiveWorkbook.Path ' current path"
    strpath = "P:\CPD\Stats\data\*.xls" ' files subdir
    myfile = Dir(strpath)
    Do While myfile <> ""
        Workbooks.Open Filename:=myfile
        myfile = Dir
    Loop
End Sub
 
Upvote 0
Thanks VoG, this worked, kind of.

I had to declare strpath and myfile:
Code:
Sub new2010find()
Dim myfile As String
Dim strpath As String
    'MyDir = ActiveWorkbook.Path ' current path"
    strpath = "P:\CPD\Stats\data\*.xls" ' files subdir
    myfile = Dir(strpath)
    Do While myfile <> ""
        Workbooks.Open Filename:=myfile
        myfile = Dir
    Loop
End Sub

There is a slight problem, the directory path contains 8 .xls files, the sciprt opens the first file, but then stops because the second file could not be found.
Did I do something wrong with declaring?
 
Upvote 0
Perhaps like this

Code:
Sub new2010find()
Dim myfile As String
Dim strpath As String
    'MyDir = ActiveWorkbook.Path ' current path"
    strpath = "P:\CPD\Stats\data\" ' files subdir
    myfile = Dir(strpath & "*.xls")
    Do While myfile <> ""
        Workbooks.Open Filename:=strpath & myfile
        myfile = Dir
    Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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
Back
Top