+ Reply to Thread
Results 1 to 5 of 5

SQL query a dynamic Range

  1. #1
    Registered User
    Join Date
    12-17-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    2

    SQL query a dynamic Range

    I would like to make a custom function in VBA which takes a dynamic range and a SQL query as input and returns the result from the SQL query.

    I have multiple data in my sheet all setup in table format and depending on my inputs that data has to be sorted and filtered.

    I know Excel has a build in filter function, but that cannot be used for my purpose.

    I have done a lot of search on this subject but haven't been able to find a solution which doesn't require a link to a saved Excel spreadsheet.

    I'm thinking of a function like this:

    Function GetSql(Input as range, SqlQuery as String)



    End Function


    Where the input to the function is
    ColA ColB ColC
    a 1 abc
    b 3 abc
    c 4 fgh
    d 6 fgh
    e 4 efr
    f 3 er
    g 2 jtr

    Does anyone have a input how to solve this?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    What would be a sample query?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-17-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: SQL query a dynamic Range

    The sample data would be like a table. First row is the header and then comes the data. The data can be text, numbers and dates.

    HeaderA HeaderB headerC HeaderD
    1 London 01-Jun-2012 1000
    2 New York 11-Sep-2010 2000
    3 Bangkok 05-Feb-2007 3000
    4 Tokyo 05-Mey-2008 4000

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: SQL query a dynamic Range

    Sample query, not data.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: SQL query a dynamic Range

    I suspect that the reason you haven't found many examples that don't read the data from a saved workbook is that MS don't recommend it. There's a long standing bug which causes a memory leak when you run SQL on an open workbook, so you shouldn't really do it.

    If all your data is in tables, can't you just import it into (insert your choice of database here) and query that? In addition, I'd be very surprised if you can't get the data you want from the tables using standard Excel.

    Perhaps you should post some examples of what you are actually trying to achieve.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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