Solved

Include exception

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

895 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

14 Experts available now in Live!

Get 1:1 Help Now