+ Reply to Thread
Results 1 to 25 of 25

Pulling in data from CSV file with changing file name

  1. #1
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Pulling in data from CSV file with changing file name

    Hi all,

    I'm using Excel 2010 and trying to write a macro to automatically 'pull' data from 4 different .CSV files into 4 different tabs of another Excel file every day. Key points are:

    1) The 4 CSV files are kept in a folder with 100+ other CSV files. Every day 4 new CSV files are dropped into the folder, they are identifiable by their file name. As an example, one of them for 22 February 2012 is called "20120222.TEXSECPOS.GRPADELP.142617117".

    2) The 9 digit number at the end of each file name is random and changes each day. The first part is the date, this also changes each day. The two sections in between remain the same each day.

    3) I'm new to programming in VBA so all I've done so far is record the following Macro for one of the files. The problem is I don't know how to adjust the macro so that it will automatically look for the file name with yesterday's date (workday) at the start. It would also have to igore the fact that the last 9 digits of the file name change randomly each day. Can anyone please help?

    Option Explicit

    Sub Macro1()
    '
    ' Macro1 Macro
    '

    '
    Workbooks.Open Filename:= _
    "F:\PB\PB Data Sheets\20120222.TEXSECPOS.GRPADELP.142617117.CSV"
    Cells.Select
    Selection.Copy
    Windows("Copy data sheet test.xlsm").Activate
    Cells.Select
    ActiveSheet.Paste
    Windows("20120222.TEXSECPOS.GRPADELP.142617117.CSV").Activate
    Application.CutCopyMode = False
    ActiveWindow.Close
    Range("A1").Select
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name

    This code shows how to loop through yesterday's CSV files with file names as described.
    Please Login or Register  to view this content.
    See if you can incorporate your code into the loop - your code would replace the MsgBox statement.

  3. #3
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    Thanks for such a neat solution. I was able to tweak it a bit but I'm not sure how to loop it for the other 3 worksheets and also how to close the dynamically named worksheet. The code I've got so far is below:

    Sub Loop_CSV_files()

    Dim folderPath As String
    Dim fileName As String
    Dim yesterday As String

    'Folder containing the .csv files

    folderPath = "F:\PB\PB Data Sheets"

    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

    yesterday = Format(Date - 1, "yyyymmdd")
    fileName = Dir(folderPath & yesterday & ".TEXSECPOS.GRPADELP.*.CSV")

    Do While fileName <> ""

    ChDir "F:\PB\PB Data Sheets"
    Workbooks.Open fileName:= _
    Dir(folderPath & yesterday & ".TEXSECPOS.GRPADELP.*.CSV")
    Cells.Select
    Selection.Copy
    Windows("PB Data Sheet Copy Maco Test.xlsm").Activate
    Cells.Select
    ActiveSheet.Paste


    End
    Loop
    MsgBox "Finished"

    End Sub

  4. #4
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: Pulling in data from CSV file with changing file name

    Use the tags around the code

  5. #5
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name

    Quote Originally Posted by adelcap View Post
    'pull' data from 4 different .CSV files into 4 different tabs of another Excel file
    Into which 4 tabs (sheets) should the 4 .csv files be placed? I can see from your first code (please surround code with [ code] [/ code] tags without the spaces) that the data from one .csv file replaces the cells on the active sheet, but does this active sheet have a particular name, or index in the list of sheets (e.g. is it the first, second, last sheet etc.)? Into which sheet should each of the other 3 .csv files be placed? Does it replace the existing data, or go below or to the side, etc.

    You haven't provided enough information for your request and I need exact answers to these questions before I can develop the code to do what you want.

  6. #6
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    The name of the mastersheet into which all data is being copied is 'PB Data Sheet.xlsm'

    These are the names of the 4 CSV files where, as before, the first part is the date that changes each day and the last part is the random number that changes each day:

    1) 20120223.TEXSECPOS.GRPADELP.142744339.CSV
    2) 20120223.TEXTDCASHB.GRPADELP.142744281.CSV
    3) 20120223.Stock Loan Detail extract.GRPADELP.142733243.CSV
    4) 20120223.Billing Summary extract.GRPADELP.142774677.CSV

    The corresponding tabs in the mastersheet which each of the above files needs to be copied into in their entirity (i.e. it overwrites the data in each of the 4 tabs with fresh information each day) are:

    1) TEXSECPOS
    2) TEXTCASHB
    3) STOCKLOANDETAIL(Extract)
    4) Billing Summary extract

    The code I have so far is below. A couple of problems with my code are:

    1) When the macro opens the directory folder in Excel the folder only shows Excel files and not CSV files. I have to manually select 'show all' files to see the CSV files as well. Opening the folder through my desktop seems to solve this as it shows all files. I've tried to do this in the macro with the

    Please Login or Register  to view this content.
    but I can't get it to work.

    2) Once the macro has opened one of the CSV files and copied the data it does not then close the window of the CSV sheet, rather it leaves it open. I'm not sure how to close the CSV window as it has a dynamic name.


    Please Login or Register  to view this content.
    Last edited by adelcap; 02-24-2012 at 11:33 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name

    With the further explanation, the Dir file loop which I first thought would do the job isn't suitable because each .csv file needs to be copied to a specific sheet. Try this instead - the code should be placed in a standard module in your master workbook.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    That seems to work very nicely. Thanks Chippy!

  9. #9
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    The macro currently looks for the file with yesterday's date on. Unfortunately the CSV files are not delivered into the folder on bank holidays and weekends. Is there a way to exlcude these dates?

    I've tried tweaking it but can't seem to get it to work. I've done this in an Excel cell forumla before where I've used a workday formula and then written the bank holiday dates in a range but I'm not sure how to transpose this to VBA. Any ideas?
    Last edited by adelcap; 02-29-2012 at 09:56 AM.

  10. #10
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name

    Do you mean if run on a Monday it should look for Friday's files? And if run on a Tuesday and Monday is a bank holiday it should also look for Friday's files?

  11. #11
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    Yes, exactly, where the following are the bank holidays it should exclude for 2012:


    26/11/2012
    27/12/2011
    02/01/2012
    19/03/2012
    06/04/2012
    09/04/2012
    07/05/2012
    04/06/2012
    05/06/2012
    12/07/2012
    27/08/2012
    25/12/2012

  12. #12
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name

    Try this code, which replaces the previous code.
    Please Login or Register  to view this content.
    As written above, the holiday dates are expected to be in cells A2:A10 on the Holidays sheet, or you could hard-code and use them like this:
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    This works great apart from the bank holidays. I've created a new sheet titled 'Holidays' and added in 28/02/2012 in A1 as a test. It still seems to pull the CSV from 28/02/2012..

  14. #14
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name

    With the single holiday date of 28/02/2012 in A1, did you change the following reference to it?
    Please Login or Register  to view this content.
    The above works for me. Run today, it looks for files with 20120227 in the file name.

    Did you try the array way of specifying the holidays, as shown?

  15. #15
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    I've used the range and it seems to be working perfectly now, thanks!

  16. #16
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    Finally, is there a way to leave Excel open and have the macro run each weekday morning at say 7am?

  17. #17
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name

    Have a look at Application.OnTime - http://www.cpearson.com/excel/OnTime.aspx.

    But what if the weekday was a bank holiday? Presumably you wouldn't want the macro to run then, but on the next proper working day? All this could be handled by the procedure called by OnTime.

  18. #18
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    This is really cool. I've got it running with a 24 hour loop. I've added a couple of sub procedures that were seperate macros so now it performs a series of procedures on a 24 hour timer. One of those things is sending an email through MS Outlook to preset recipients. Using the following:

    Please Login or Register  to view this content.
    This would be fully automated if before sending the email it didn't request permission - a message box pops up asking to allow or deny email sending ('A program is trying to send an email on your behalf...'). Does anyone know how to add to the macro to just allow this?
    Last edited by adelcap; 03-02-2012 at 10:14 AM.

  19. #19
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name


  20. #20
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    This is great, now works perfectly. Thanks Chippy

  21. #21
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    Hi Chippy,

    This is a brilliant piece of code. What I've found however is that some of the CSV files have columns with dates in which are also formatted as dates. When the macro runs it copies and pastes these columns but I get the following, notice how the date format switches on the 13th day.

    Business Date
    03/01/2012
    03/02/2012
    03/03/2012
    03/04/2012
    03/05/2012
    03/06/2012
    03/07/2012
    03/08/2012
    03/09/2012
    03/10/2012
    03/11/2012
    03/12/2012
    13/03/2012
    14/03/2012
    15/03/2012
    16/03/2012
    17/03/2012

    I am able to resolve this by manually copying and pasting the column as special values and then the formatting. Is there a way of tweaking the VBA code to do this automatically?

    I've tried playing around with it but the code is so clever I can't even see where the paste command is?

    Here is the code:

    Please Login or Register  to view this content.
    Last edited by adelcap; 04-02-2012 at 05:36 AM.

  22. #22
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name

    Quote Originally Posted by adelcap View Post
    the date format switches on the 13th day.
    This is a common problem with British dates in .csv files. Excel interprets them as USA dates. Fortunately the solution is simple. Change the Workbooks.Open line to:
    Please Login or Register  to view this content.
    I can't even see where the paste command is
    The Cells.Copy line specifies a Destination argument (see 'Copy method as it applies to the Range object' in the VB Help), so it doesn't paste separately (via the clipboard) but copies the data in one go. Because of this the Application.CutCopyMode = False line is unnecessary and you can delete that line.

  23. #23
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    Chippy, thank you! Genius.

  24. #24
    Registered User
    Join Date
    02-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: Pulling in data from CSV file with changing file name

    One further question, the current code copies only the front sheet from 'File A' to a specificed sheet in 'File B'.

    Is there a way of copying more than one sheet from 'File A' to more than one specificed sheet in 'File B'?

    e.g. File A has 5 sheets, labelled Sheet 1 through to Sheet 5.

    I'd like the VBA code to copy each of the sheets to specified destination sheets in 'File B'?

  25. #25
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Pulling in data from CSV file with changing file name

    Yes, this could be done, using a suitable data structure to hold the sheet names and writing code according to the chosen data structure. You could extend the idea of the csvMap array used in the existing code. It depends on how the sheets (name or index) in 'File A' are related to the sheets (name or index) in 'File B' and whether this is consistent for each and every 'File A' and 'File B'.
    Post responsibly. Search for excelforum.com

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1