+ Reply to Thread
Results 1 to 2 of 2

Trying to edit query from excel to sql

  1. #1
    Registered User
    Join Date
    04-21-2013
    Location
    Norway
    MS-Off Ver
    Excel 2016
    Posts
    15

    Trying to edit query from excel to sql

    Hi!

    I have a SQL database that I connect to through excel to do some analysis and reports, but I'm having trouble when trying to simplify the connection-prosess. I have a code that looks a bit like this in my excel-macro:

    With Sheets("Grunndata").ListObjects.Add(SourceType:=0, Source:="ODBC;DSN=storagedb" _
    , Destination:=Range("Grunndata!$A$1")).QueryTable
    .CommandText = Array( _
    "SELECT transfer.id, transfer.delivnr, transfer.pallenr" & Chr(13) & "" & Chr(10) & "FROM DBA.transfer transfer" & Chr(13) & "" & Chr(10) & "WHERE (transfer.delivnr=16365)" )

    And what I want to do, is that on refresh, the number at the end (16365) must come from a certain cell. Change the deliverynr in a cell, hit refresh, and the query against the sql database starts with the new deliverynr...

    Thanks for any help.

    Kjetil

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Trying to edit query from excel to sql

    Can you not just create a Long variable and assign the range?

    Please Login or Register  to view this content.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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