Solved

Include exception

Posted on 2016-07-18
2
40 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 48

Expert Comment

by:Rgonzo1971
ID: 41718178
Hi,

Do you know which error it is?

Regards
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

744 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