Solved

Include exception

Posted on 2016-07-18
2
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

730 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