Small Macro Tweaks and Error Handler in VBA

zaska

Well-known Member
Joined
Oct 24, 2010
Messages
1,046
Hello,

I am having the following problems while executing the code. I kindly request support for rectifying the same.

1. Error Handling : If the specified input file doesn't exist then the VBA code is generating error.

2. I want to speed up this VBA using application.screenupdating = False. But i don't know where exactly i should use it in the code.

3. I want to close the Opened .Csv files before running into the next loop.

I have almost 5000 files to be processed .

Code:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+a
'

 Dim strPath As String, otrPath As String, startPath As String, Endpath As String, i As Long, buffer As String, startDate As Date
    
    startPath = "E:\BSE\CM\" 'current directory
    Endpath = "E:\BSE\OP\"   'output path
    startDate = "14/06/2010" 'DD/MM/YYYY
    
    Do While buffer <> "2011-06-04"
        buffer = Format$(startDate, "YYYY-MM-DD")
        strPath = startPath & buffer & "_BSECM.csv"  'full path of the file you want to open
        otrPath = Endpath & buffer & ".txt"
        startDate = startDate + 1 'add one day
        'Your code
 
    Workbooks.Open Filename:=(strPath)
    Columns("I:I").Delete Shift:=xlToLeft
    Rows("1:1").Delete Shift:=xlUp
    Columns("B:B").Insert Shift:=xlToRight
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="-", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
    Columns("B:B").Delete Shift:=xlToLeft
    Columns("C:F").NumberFormat = "0.00"
    Columns("B:B").NumberFormat = "yyyymmdd"
    ActiveWorkbook.SaveAs Filename:=(otrPath), CreateBackup:=False

    Loop
    
End Sub


Thank you
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hello,

1. Error Handling : If the specified input file doesn't exist then the VBA code is generating error.

2. I want to speed up this VBA using application.screenupdating = False. But i don't know where exactly i should use it in the code.

3. I want to close the Opened .Csv files before running into the next loop.

1. If Len(Dir(strPath))>0 Then ...

If this is true, you know the file exists.

2. Just before the first line of code where you will interact with the user interface, like opening files for instance. If you are assigning values to variables, this setting has no use.

3. ActiveWorkbook.Close
 
Upvote 0
Sir,

I made the suggested changes. I am getting Close, Save Dialog box everytime . How to avoid this?

Is there anyway to stop this code in between , because it will take lot of time to process 5000 files.

Code:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+a
'

 Dim strPath As String, otrPath As String, startPath As String, Endpath As String, i As Long, buffer As String, startDate As Date
    
    startPath = "E:\BSE\CM\" 'current directory
    Endpath = "E:\BSE\OP\"   'output path
    startDate = "14/06/2010" 'DD/MM/YYYY
    
    Do While buffer <> "2011-06-04"
        buffer = Format$(startDate, "YYYY-MM-DD")
        strPath = startPath & buffer & "_BSECM.csv"  'full path of the file you want to open
        otrPath = Endpath & buffer & ".txt"
        startDate = startDate + 1 'add one day
        'Your code
    Application.screenupdating = false    
    If Len(Dir(strPath)) > 0 Then
    Workbooks.Open Filename:=(strPath)
    Columns("I:I").Delete Shift:=xlToLeft
    Rows("1:1").Delete Shift:=xlUp
    Columns("B:B").Insert Shift:=xlToRight
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="-", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
    Columns("B:B").Delete Shift:=xlToLeft
    Columns("C:F").NumberFormat = "0.00"
    Columns("B:B").NumberFormat = "yyyymmdd"
    ActiveWorkbook.SaveAs Filename:=(otrPath), CreateBackup:=False
    ActiveWorkbook.Close
    End if

    Loop
    
End Sub


Thank you
 
Upvote 0
Add the lines in red to your code:

Code:
    [COLOR="Red"][B]Application.DisplayAlerts = False[/B][/COLOR]
    ActiveWorkbook.SaveAs Filename:=(otrPath), CreateBackup:=False
    ActiveWorkbook.Close
    [COLOR="Red"][B]Application.DisplayAlerts = True[/B][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,181
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top