+ Reply to Thread
Results 1 to 8 of 8

Looking for an open .csv file

  1. #1
    Registered User
    Join Date
    12-06-2014
    Location
    Manchester, UK
    MS-Off Ver
    2010
    Posts
    5

    Looking for an open .csv file

    Hi everyone,

    I'm trying to write a macro that will swap from Excel to an *open* CSV file, and copy its contents into the current spreadsheet.

    The CSV file doesn't have a fixed location, or have a fixed file name, so I can't simply loop through files in a folder, or call the filename directly.

    I was hoping to use:

    Please Login or Register  to view this content.
    but it seems like the CSV file isn't counted as a Workbook.


    I could get the user to save the CSV file somewhere fixed, but I want to try and make it user-interaction free as possible.


    If it helps, the CSV file will be the only one open (i.e. there is no chance of getting mixed up between two) and it's file name will always begin with "LM".


    Any ideas?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looking for an open .csv file

    Hello ryanarbabi,

    Since you are not seeing the CSV files in the Workbooks collection then the CSV files must be opening in another application. Typically, Notepad is used to open text files by default.

    Is the CSV file open in Notepad or some other application? Which application is being used?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Looking for an open .csv file

    @Leith - alternatively, perhaps the CSV opens in another instance of Excel?
    He might be able to find it through Excel.Application, but without knowing the full name, I don't know how to loop through the instances. Can you help?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looking for an open .csv file

    Hello jasperD,

    The OP could have the CSV open in another instance of Excel though I have found this to be an uncommon situation.

    Here is the code for a macro to find other instances of Excel and a macro to activate those instances. This is for 32 bit Office.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-06-2014
    Location
    Manchester, UK
    MS-Off Ver
    2010
    Posts
    5

    Re: Looking for an open .csv file

    The CSV file opens in Excel.

    If I reference the file name directly, it works fine, however I have no control over what the file name will be other than beginning with "LM"

    Ryan

  6. #6
    Registered User
    Join Date
    12-06-2014
    Location
    Manchester, UK
    MS-Off Ver
    2010
    Posts
    5

    Re: Looking for an open .csv file

    So I don't think the different instance of Excel is the problem, they both show up in VBA:

    Untitled.png

    I made the little program shown in the screenshot to practice, but it returns "Cut Pad Price Calculator v4.23 PRO" twice, and that's it.

  7. #7
    Registered User
    Join Date
    12-06-2014
    Location
    Manchester, UK
    MS-Off Ver
    2010
    Posts
    5

    Re: Looking for an open .csv file

    I've tried @Leith's code:

    ActivateInstance 0 (Nothing happens)
    ActivateInstance 1 (Nothing happens)
    ActivateInstance 2 (Last active window maximizes)
    ActivateInstance 3 (Out of range error)

    Can I target an open file using *.csv?

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looking for an open .csv file

    Hello ryanarbabi,

    Here is the code to assign the CSV workbook to a variable. Since I have no idea of what the final layout looks like, you will have to fill some blanks.
    Please Login or Register  to view this content.

+ 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. A file iis found if I open Excel FIRST, but not if I open the file by double-clicking
    By Ed_Collins in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2013, 01:52 AM
  2. [SOLVED] open variably named file, copy/paste data into consolidation file, open next file in list
    By sllawrence1968 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-29-2012, 09:49 PM
  3. VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with code
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2011, 12:12 AM
  4. Replies: 1
    Last Post: 08-05-2006, 04:59 PM
  5. [SOLVED] Open a file do a macro ( made) and open next succesive file
    By SVTman74 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-21-2006, 05:20 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