Filter importing cvs file?

Posted on 2012-09-06
Last Modified: 2012-09-07
Is there a method where I can filter an importing/updating cvs file?
I have multiple users and would like to only import thier data from the cvs.
The data has been imported to an excel and it refreshes from there.
Question by:Euro5
    LVL 92

    Expert Comment

    by:Patrick Matthews
    Can you post a more complete description of what you need to do, as well as (sanitized) samples of the CSV and Excel files?

    Author Comment

    Each user has a copy of the same file that imports the same csv file.
    However, what I need is for the file to only import the number entered in cell C1.
    This number would correspond to column A in csv file.

    Is this enough description? Thanks so much!
    LVL 24

    Expert Comment

    Yes you can.
    If you open up MS Query and use this to open up the csv file you can use SQL to select the data you want... the below code is modified from recorded MS Query...

    Dim cmd As String
    Dim src As String
    Dim dsp As String
        dsp = "txt_" & Format(Now(), "hhmmss")
        src = "ODBC;DefaultDir=" & ThisWorkbook.Path & ";Driver={Microsoft Text Driver (*.txt; *.csv)};DriverId=27;FIL=text;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"
        cmd = "SELECT *" & Chr(13) & "" & Chr(10) & "FROM `filename.csv` `filename`" & Chr(13) & "" & Chr(10) & "WHERE (filename.Fieldname =  'SearchCriteria')"
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=src, Destination:=Range("$A$1")).QueryTable
            .CommandText = cmd
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = dsp
            .Refresh BackgroundQuery:=False
        End With

    Open in new window

    LVL 24

    Expert Comment

    to extract a single value:

    strPath = ThisWorkbook.Path & "\"
    Set cn = CreateObject("ADODB.Connection")
    strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
    cn.Open strcon
    strSQL = "SELECT * FROM IPSAMPLE.csv;"
    Dim rs As Recordset
    Dim rsARR() As Variant
    Set rs = cn.Execute(strSQL)
    thevalue = rs("Region").value
    set cn = nothing

    Open in new window


    Author Comment

    How do I open up MS Query?
    LVL 24

    Accepted Solution

    MS Query is from Data > From other Sources > MS Query
    Then you can point it at csv files.

    Author Closing Comment

    Thanks so much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
    This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
    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 will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

    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

    11 Experts available now in Live!

    Get 1:1 Help Now