+ Reply to Thread
Results 1 to 6 of 6

Importing csv file with variable name

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Importing csv file with variable name

    Is it possible to code for data within a csv file to be copied into a worksheet of a named workbook, where the csv file has a variable name, i.e. where the csv file is exported from a 3rd party application and the first file exported has the name "Export", then "Export(1), then "Export(2), etc.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Importing csv file with variable name

    Something like the following

    Sub Latest()
    
        Dim fso, ofileItem, oSource, stDirectory As String, stLatest As String, dtLatest As Date, stExt As String
        
        stDirectory = "C:\users\temp\" ' Change folder path as necessary
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set oSource = fso.GetFolder(stDirectory)
        
        For Each ofileItem In oSource.Files
            stExt = fso.getextensionname(ofileItem)
            If stExt = "xlsx" Then
                If FileDateTime(stDirectory & ofileItem.Name) > dtLatest Then
                    stLatest = ofileItem.Name
                    dtLatest = FileDateTime(stDirectory & ofileItem.Name)
                End If
            End If
            
        Next ofileItem
        
        Set fso = Nothing    
        
        Workbooks.Open FileName:=stDirectory & stLatest
        ' add code to copy the csv sheet
        ActiveWorkBook.Close
        ;add code to paste to the relevant sheet in the current workbook
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Importing csv file with variable name

    Hi Richard

    That works a treat for selecting the latest file. Is there a way that the user could select the csv file they require? Would it be via a dialogue box?

  4. #4
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Importing csv file with variable name

    Richard

    Sorry. I was given incorrect information. It seems that the csv files are opened one after the other and remain open, so the user would have to select the relevant csv file to copy and close it once that's been done.

    There could be a number of csv files open at any one time.

    I think it would be easier for them to simply "Alt + Tab" to select the relevant csv file and then manually select all the data on the single sheet that is displayed, before pasting it into the relevant destination workbook & sheet.

    Unless you can suggest if there is another way....I've got no clue!

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Importing csv file with variable name

    So are you saying that the user doesn't want to open just the csv file with the latest date/time stamp, they want to pick a file and open that?

    And if so, and having done that for one file do they want to open another file leaving the first one also open in memory?

  6. #6
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Importing csv file with variable name

    They have the destination workbook open, run two reports from a 3rd party application which exports them as csv files, e.g. "reportA.csv" & "reportB.csv", each containing a single sheet of data.

    They copy each sheet of data, pasting it into a particular worksheet on the destination workbook before running a macro to manipulate the pasted data, creating a new workbook. The two csv files are then closed, the data in the destination workbook cleared, and the process repeated, where the exported csv files are named "reportA(1)" & "reportB(1)" and so on until the 3rd party application is closed and reopened when the csv names revert to "reportA" etc.

    I've written code to manipulate the data and run the necessary report but was looking at whether there's a way to import the csv files(s) data in to the relevant worksheets.

    But as the csv files are a)Open, & b)Contain a variable name, i.e. "filename", then "filename(1)", then "filename(2)" I'm struggling to work out whether it could be done.

    Additionally the user ideally should be able to see which csv file they're selecting, to prevent them from entering the wrong one into the relevant worksheets in the destination workbook.

    Does that make sense?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Importing a text file by vba referring to a cell for file name & file path
    By Vivek2705 in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 12-01-2017, 08:54 AM
  2. Importing text file. Does entire file get read into memory first?
    By sumdumgai in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-11-2016, 07:28 PM
  3. Importing file as temp and copy paste info into main file!!! HELP PLEASE
    By cory0789 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-05-2015, 02:01 PM
  4. VBA code - importing text file to excel - variable filename!
    By 2010snowbeach in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-10-2014, 01:16 AM
  5. VBA Importing Text File makes the file longer to load everytime
    By Hudas in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-15-2014, 11:26 AM
  6. Error 91 object variable block variable not set when importing a CVS file
    By ReportBuilder2011 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2011, 11:48 AM
  7. Importing text file, only option to edit existing file
    By smokey99 in forum Excel General
    Replies: 8
    Last Post: 04-26-2006, 04:08 PM

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