Solved

Include exception

Posted on 2016-07-18
2
46 Views
Last Modified: 2016-07-19
Hello,

I have a VBS script that basically open a .xls file and runs the macro in it. It runs for all the Excel files that are in that specific location. The problem is when there is any error in one of the excel file the macro stops running for the rest of the files. How can I write an exception to skip if any error found and go to the next file?


5. Vbscript file  RUN_MACRO.VBS

Dim xlApp, xlBook

Set xlApp = CreateObject("Excel.Application")
'~~> Change Path here
Set xlBook = xlApp.Workbooks.Open("C:\dq_export\Macro_run.xls", 0, True)
xlApp.Run "Macro_run!Loopfiles"
'xlBook.Close
'xlApp.Quit

Set xlBook = Nothing
Set xlApp = Nothing

'WScript.Echo "Finished."
'WScript.Quit


6. Excel Macro code (this code is located inside of the the macro_run.xls file) 

Sub Loopfiles()
Dim wb As Workbook
Dim myPath As String
Dim myfile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

myPath = "c:\dq_export\csv\"
myfile = Dir(myPath & "\" & "*.csv")


'Loop through each Excel file in folder
  Do While myfile <> ""
    'Set variable equal to opened workbook
      Set wb = Workbooks.Open(Filename:=myPath & myfile)

';;;;;;;;;;;;;;WRITE YOUR CODE HERE
    FormatResult
    testMacro



     ' wb.Close SaveChanges:=True
 With wb
        .SaveAs Replace("c:\dq_export\excel\" & myfile, ".CSV", ".xls"), 50 'UPDATE:
        .Close True
End With
    
    'Get next file name
      myfile = Dir
  Loop

'Message Box when tasks are completed
'  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    Application.DisplayAlerts = False
    Application.Quit

End Sub

Open in new window

0
Comment
Question by:angel7170
2 Comments
 
LVL 15

Accepted Solution

by:
WalkaboutTigger earned 500 total points
ID: 41717941
Have you tried the simple construct of

On Error Resume Next

at the beginning of your script, or do you want to capture which XLS file is generating an error?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41718178
Hi,

Do you know which error it is?

Regards
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now