Looping Help needed for a Herculus Task

zaska

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

I recorded and tweaked the below macro to process a .csv file which is named as " 2000-06-14_BSECM ". I have about 5000 .csv files to process the same thing.

I kindly request any one to help me looping the below code so that i could avoid the Hercules task of processing each .csv file.

Except the Date of the .csv the rest of the .csv name remains constant.

Is it possible to have a calender in excel sheet so that i can choose the dates to process the .csv files.

I would also like to know whether this code can be further tweaked so that the loop runs faster.


HTML:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+a
'
    ChDir "C:\Users\pc\Desktop\BSE"
    Workbooks.Open Filename:="C:\Users\pc\Desktop\BSE\2010-06-14_BSECM.csv"
    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
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\pc\Desktop\BSE\2010-06-14_BSECM.csv", FileFormat:=xlCSV, _
        CreateBackup:=False
    ActiveWindow.Close
End Sub

Thank you
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi, I think you would need to explain more about your filenames.
like from which date to which date are in the folder?
Are they all in the same folder?
What are their names? (like a rule, MM_DD_YYY_Blarg.csv?)

and where should the csv files be saved to? or added to?
Does each csv file represent each worksheet of the same workbook?
or does it just represent each workbook,..
 
Upvote 0
Hi,

Let me explain further

File Name Format : "YYYY-MM-DD_BSECM.csv "
All the Files are stored in the Same Directory : NewFolder in D Drive ( I can change the path as required)
Each .Csv file should be saved with the same name after processing in a text format or .csv format

I can change the path of Output folder as required.

Totally I have 5000 .csv files. Starting from date " 2000-01-01 "


Thank you for the reply and any help in this regard will save me a lot of time.
 
Upvote 0
Hello zaska,
Here is the snippet of code that changes the files you want to open.
If any of the file names are missing in the directory, I believe the code will error out and stop executing so you must add an error handler.
I have not yet had a chance to incorporate this code into yours.

Code:
    Dim strPath As String, startPath As String, i As Long, buffer As String, startDate As Date
    
    startPath = "C:\Users\pc\Desktop\BSE\" 'current directory
    startDate = "1/1/2000" '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
        startDate = startDate + 1 'add one day
        'Your code
    Loop
 
Upvote 0
Sir,

I have made the following changes & i kindly request to correct the code.

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

 Dim strPath As String, startPath As String, i As Long, buffer As String, startDate As Date
    
    startPath = "E:\BSE\CM\" 'current directory
    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
        startDate = startDate + 1 'add one day
        'Your code
    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
[COLOR=Red]    ActiveWorkbook.SaveAs Filename:="E:\BSE\OP\startPath & buffer & "_BSECM.csv", FileFormat:=txt,
        CreateBackup:=False
    ActiveWindow.Close[/COLOR]
      Loop
    
End Sub

Thank you
 
Upvote 0
Hi,

Can anyone help me in correcting the syntax errors in the below code

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

 Dim strPath 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
        startDate = startDate + 1 'add one day
        'Your code
        
    How  to Open the Above file ?
    
    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
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:= Endpath & ".txt", FileFormat:=txt,
        CreateBackup:=False
    ActiveWindow.Close

    Loop
    
End Sub

thank u
 
Upvote 0
Hi,

I thank you for the kind help. Finally i am able to finish the work.

Regards,

Zaska
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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