Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Querying CSV file through VBA without knowing the header

Posted on 2013-06-04
4
Medium Priority
?
2,314 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

597 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