?
Solved

Querying CSV file through VBA without knowing the header

Posted on 2013-06-04
4
Medium Priority
?
2,247 Views
Last Modified: 2013-06-04
Hi,

I'm creating a macro that queries a CSV file that a user chooses, and saves the result of only the lines that meet a certain criteria.

The CSV file contains 7 columns, but 2 of which the headers are dates so they vary.  I need a way to be able to either find out the header names before running the query, or specify in the query through a header number.

e.g.
My file contains the following CSV file:
Curr,Days,1/1/2013,1/2/2013,Change,%Change
USD,1,100,100,0,0
JPY,1,1000,1000,0,0
....

Below is my code
objConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & dSource & ";" & _
        "Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";Persist Security Info=False"
objConn.Open



SqlQry = "SELECT * FROM " & dfile & " " & _
            "WHERE ([% Change] >= 5 OR [% Change] <= -5) "

Rcdset.Open SqlQry, objConn

For i = 0 To Rcdset.Fields.Count - 1
    data_ws.Cells(1, i + 1).Value = Rcdset.Fields(i).Name
Next i

data_ws.Cells(2, 1).CopyFromRecordset Rcdset

Open in new window

So basically I'm looking for anything that has a 5% change, and I also need to look for any that didn't have data on today's column.  So in this case, it would be WHERE 1/2/2013 = '' but how can I chose this column/header when the value is constantly going to change?
0
Comment
Question by:iamnamja
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39219160
Are the fields always in the same order? If so, perhaps specify HDR=No in the connection and use F4 for today's field and F6 for the % change field. Then you just need to skip the first record in your processing.
0
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 2000 total points
ID: 39219178
Hi,

If you change the "HDR=Yes" part of the connection string to "HDR=No" you should be able to refer to the columns of data in your SQL SELECT statement as follows:

SELECT [F1], [F2], [F3], [F4],.... [F"N"] {where "N" is how many columns are present within the file}
FROM {filename}

(i.e. use the "SELECT [F{column number}]" syntax instead of "SELECT *")

However, if you wish to know the names of the headings to the columns you should either open the file once to retrieve the first few rows & write code to then extract the heading titles you require before re-querying the file contents (for a second time) with named headings...

...or import the first two records only (SELECT TOP 2 * FROM...) still with the "HDR=No" attribute within the connection string.  You can then review the resultset to gain the column headings.

BFN,

fp.
0
 

Author Closing Comment

by:iamnamja
ID: 39219189
Thanks while waiting, I did just this... I queried it twice. Once to get the header, and once to use the header to query it.  Thanks!
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39219257
Sorry I took seven minutes to reply! ;)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question