+ Reply to Thread
Results 1 to 11 of 11

SQL - query on excel cells rather than write the query text?

  1. #1
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    SQL - query on excel cells rather than write the query text?

    Dear all,

    I'm using one Excel macro to do some SQL queries on a lot of excel files that I have. This file works perfectly, but I want to know if it is possible to do references to cells in the SQL query section of this file.

    For example, on this query:
    Please Login or Register  to view this content.
    Is it possible to select the date numbers from Excel cells itself, rather than write them on the code? For example something like this:
    Please Login or Register  to view this content.
    Please if this is possible let me know how, because it will help me a lot!

    Thank you
    Attached Files Attached Files
    Last edited by Pepe Le Mokko; 02-11-2020 at 09:56 AM. Reason: Simplfied title

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Help SQL Macro - Is it possible to query on excel cells rather than write the query te

    Yes but you can't put the cell references into the sql directly. Something like:

    sql = "select DATE, PAYMENT_FORM,VALUE from [Venda$] where DATE BETWEEN '" & worksheets("sheetname").Range("H1").Value & "' AND '" & worksheets("sheetname").Range("I1").Value & "' AND CARTEIRA_CLIENTE = 'DIAMANTE'"

    I also don't know if you are querying a database or a worksheet, but I typically use # instead of ' in my queries around dates. You also don't need to actually reference the sheet if it is for sure that those ranges will always be on the active sheet.
    Last edited by Arkadi; 02-10-2020 at 01:12 PM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Help SQL Macro - Is it possible to query on excel cells rather than write the query te

    Quote Originally Posted by Arkadi View Post
    Yes but you can't put the cell references into the sql directly. Something like:

    sql = "select DATE, PAYMENT_FORM,VALUE from [Venda$] where DATE BETWEEN '" & worksheets("sheetname").Range("H1").Value & "' AND '" & worksheets("sheetname").Range("I1").Value & "' AND CARTEIRA_CLIENTE = 'DIAMANTE'"

    I also don't know if you are querying a database or a worksheet, but I typically use # instead of ' in my queries around dates. You also don't need to actually reference the sheet if it is for sure that those ranges will always be on the active sheet.
    Dear Arkadi,

    Thank you for your reply, but unfortunatly I'm still not able to do what I want. I'm querying a worksheet, and using your query string I'm getting this error: "Data type mismatch in criteria expression."
    Do you know what might be?

    Thanks again
    Last edited by ropbasuel; 02-10-2020 at 03:39 PM.

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Help SQL Macro - Is it possible to query on excel cells rather than write the query te

    Any chance you can provide a file with the relevant code and some sample data? It would make it easier to figure out the cell formatting for the dates... I assume H1 and I1 are cells formatted as date as well as the column "DATE"?

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Help SQL Macro - Is it possible to query on excel cells rather than write the query te

    When retrieving date value from cell and using it in SQL code. I'd recommend using Value2 to obtain value from cell, rather than using .Text or .Value.
    Value will return inconsistent result depending on formatting applied to cell.

    If cell holds datetime vlaue (i.e. Now()) and .Value is used...
    If cell is formatted for date - It will return both date and time formatted to system setting.
    If cell is formatted for time - It will return double value representing datetime.
    etc.

    I'd recommend something like below, if only using date.
    Please Login or Register  to view this content.
    "yyyy-Mm-Dd" format is universally accepted by SQL dbs.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  6. #6
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Help SQL Macro - Is it possible to query on excel cells rather than write the query te

    Dear Arkadi and CK76,

    Thank you so much for your replies!!

    Please find attached one query file and one sample database. Before starting messing with dates (which is always tricky) lets start with one simple variable. In the query file if i run the command:

    Please Login or Register  to view this content.
    I get some results. But if I try to instead of using value "2", try to use a cell that has a "2" I got the previous error that I have stated on my last post. I'm using this code:

    Please Login or Register  to view this content.
    Before start messing with dates I think the main problem is that I can't reference a cell on the code itself. Can someone have a look at it please?

    Again, thank you so much!

    PS - If you are testing my file, please check/edit the path to the database spreadsheet file on sheet "Inf_BD" of the query file.
    Attached Files Attached Files

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Help SQL Macro - Is it possible to query on excel cells rather than write the query te

    I did not realize that you are building the query in a cell as well... that doesn't really work out so well when trying to put in cell references. Instead you should use a formula that builds the actual sql statement. So in G5 you would do something like this: (note the # symbol around dates)

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    That will then result in:

    select * from [Venda$] where PRODUTO > 2 AND DATE BETWEEN #2015-08-16# AND #2015-08-20# (in my case because of the dates i put in H1 and I1 to test)

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Help SQL Macro - Is it possible to query on excel cells rather than write the query te

    My personal preference is to use "Microsoft.ACE.OLEDB.12.0" rather than Excel Driver.

    Ex:
    Assuming that you have Arkadi's formula in G1.

    Code like below.
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: SQL - query on excel cells rather than write the query text?

    Dear Arkadi, and CK76,

    First of all my apologies for the late reply.
    I have tested both of your solutions and they work perfectly!!! This will help me a lot and save me a lot of time!

    I want to thank you both for your time and wiliness to help!

    Wish you all the best!

    Best Regards

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: SQL - query on excel cells rather than write the query text?

    Glad we could help you solve the issue
    Thanks for the feedback, rep, and marking the thread as solved!

  11. #11
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: SQL - query on excel cells rather than write the query text?

    You are welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Excel Power Query Refresh or Access Query - 2nd Query Run is faster
    By Steveapa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-03-2020, 10:16 AM
  2. Replies: 1
    Last Post: 09-08-2015, 08:12 PM
  3. how to write query in excel
    By nramulu99 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-06-2014, 03:02 AM
  4. [SOLVED] Cannot write a formula in Excel 2010 to answer query and give a final COUNTIF number
    By Kevin Fisher in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-09-2014, 04:04 PM
  5. Web Query Help? - Query Picture Title or Alt Text on a webpage?
    By teamtrav in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-23-2012, 06:33 PM
  6. How to write Query in Excel (MS Query)
    By acsishere in forum Excel General
    Replies: 4
    Last Post: 04-21-2008, 04:11 PM
  7. query will not write to excel
    By Klutzz in forum Excel General
    Replies: 0
    Last Post: 03-26-2005, 10:06 AM

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