Solved

Include exception

Posted on 2016-07-18
2
52 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 50

Expert Comment

by:Rgonzo1971
ID: 41718178
Hi,

Do you know which error it is?

Regards
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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,…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

856 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