Solved

Querying CSV file through VBA without knowing the header

Posted on 2013-06-04
4
1,905 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
  • 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now