+ Reply to Thread
Results 1 to 3 of 3

VBA code possible to extract values from .CSV files?

  1. #1
    Registered User
    Join Date
    10-19-2011
    Location
    East Coast
    MS-Off Ver
    Office 365
    Posts
    33

    VBA code possible to extract values from .CSV files?

    Hello,

    I have the following code (below) that navigates me to a folder containing a set of .CSV files. I target two values (present in the same cells in each file) to append to the Active Workbook I'm running the code from. I thought I had an issue working accessing files stored on OneDrive, but that wasn't the issue. If I try to access the target values from the .CSV files, I get a '#REF!' error. However, if I run a script that changes the .CSV file format to .XLSM format, I can run this code and my target values are populated properly. I can even manually change the file extensions back to .CSV and it still works (I imagine because it is still interpreted as an .XLSM file). I do have code to change all of the original .CSV files over to .XLSM file formats, but it makes other steps in my reporting stream more difficult.

    My bottom-line question is: Can the code below be adjusted so that I can successfully grab my target values from the original .CSV files inside the 'Orig_CSV_Rpts' folder (instead of having to first convert them to .XLSM files)? The code below results in '#REF!' errors for the values I'm targeting in 'A14' & 'B14 in each of the .CSV files.

    Thank you!

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    05-10-2016
    Location
    Cordoba, Argentina
    MS-Off Ver
    2016
    Posts
    27

    Re: VBA code possible to extract values from .CSV files?

    Hi Patdool, it is me again,
    Now I understand what is happening, you can reference a cell in a closed spreadsheet by its path file sheet and cell address like in
    ='C:\Temp\[reftocsv.xlsx]reftocsv'!$A$1

    But you cannot reference a piece of a .csv file like with a formula like
    ='C:\Temp\[reftocsv.csv]reftocsv'!$A$1

    You will always get a REF error, because .csv file have no rows, columns nor cells, a .csv is just a text file
    that Excel transforms into rows and columns when it opens it, but when is closed is just text,

    You can try to open each one save as .xls* with VBA and then reference a cell from the .xls* file
    Or you could try open them the txt/csv file with VBA go to the 14th record and get the information from the line counting commas/semicolons
    I do not know what your csv file is using as a field separator.

    With
    open_file_code.gif

    Also a good article in https://stackoverflow.com/questions/...ba-performance
    And a real good one very structured to solve your problem https://sitestory.dk/excel_vba/csv-file-import.htm

    Cheers
    Sergio
    Last edited by sergiomabres; 02-17-2019 at 03:07 PM.

  3. #3
    Registered User
    Join Date
    10-19-2011
    Location
    East Coast
    MS-Off Ver
    Office 365
    Posts
    33

    Re: VBA code possible to extract values from .CSV files?

    Hi Sergio - thank you very much for your response. I'm glad I at least understand why I could not make this code behave while attempting to extract information from .CSV files. I will be converting the files to .XLS* files, as that will ultimately be easier for me to obtain my targeted data rather than messing with these lengthy code examples that I do not understand quite as well. Thank you again!

+ 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. Loop Code to extract information from all files in a folder to a mastersheet
    By flaire14 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2017, 08:34 PM
  2. [SOLVED] Code to extract Data from a list of Files
    By hammer2 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-29-2016, 04:50 AM
  3. [SOLVED] Extract a few values from several .csv-files and bundle them in a single output-file
    By Jeroen606 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2013, 01:05 AM
  4. Code to extract files names that have no password
    By tommyryan1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-06-2012, 01:42 PM
  5. Extract Values from external Files
    By Sultix in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-12-2012, 02:39 PM
  6. VBA Code to extract and save sections of worksheet to seperate files
    By Zyphon in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-17-2007, 10:57 AM
  7. Using condition to extract data by matching values from 2 different Excel files
    By schellam in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-29-2005, 04:43 PM

Tags for this Thread

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