+ Reply to Thread
Results 1 to 9 of 9

SELECT based on a column in Excel

  1. #1
    Registered User
    Join Date
    11-27-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    SELECT based on a column in Excel

    Hello,

    I've constructed a code in VBA where I SELECT all orders from the last month and then I can do some analytics with this data. But I also want to fetch how these orders have been invoiced. What I've done is that I create a column in Excel where all the orderid's are that I want invoice data about. I can copy this column into my SQL-code manually with no problem but I want to make it automatic. What I'm trying to do is somthing like:

    First i declare
    Please Login or Register  to view this content.
    (because I want the data I save in this variable to be saved in between subs.)

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    But I get the compile error: Object required and the problem is rngOrderId in my sqlStr. How can I solve this problem?

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: SELECT based on a column in Excel

    You have a variable 'row'.

    In the code you've posted no value has been assigned to it.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    11-27-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: SELECT based on a column in Excel

    Quote Originally Posted by Richard Buttrey View Post
    You have a variable 'row'.

    In the code you've posted no value has been assigned to it.
    Well the row variable is working. It's been declared and I have no problems using it elsewhere so that's not the problem. The problem is the rngOrderId variable!

  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: SELECT based on a column in Excel

    You'll need to create a comma delimited string from the values in the range rngOrderId and use that in the IN clause of the SQL statement.

    Something like this might work for numeric IDs.
    Please Login or Register  to view this content.

    PS The error was probably because you used Set for the, string, SQL statement, not the variable rngOrderId.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    11-27-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: SELECT based on a column in Excel

    Quote Originally Posted by Norie View Post
    You'll need to create a comma delimited string from the values in the range rngOrderId and use that in the IN clause of the SQL statement.

    Something like this might work for numeric IDs.
    Please Login or Register  to view this content.

    PS The error was probably because you used Set for the, string, SQL statement, not the variable rngOrderId.
    Alright, I got over that problem but now it's giving me an 1004-error and marking that the problem lies in
    Please Login or Register  to view this content.
    Whereas:
    Please Login or Register  to view this content.
    The error I get is:
    Run-time error '1004':
    Application-defined or object-defined error
    Last edited by Sherk; 09-15-2014 at 11:04 AM.

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: SELECT based on a column in Excel

    Quote Originally Posted by Sherk View Post
    Well the row variable is working. It's been declared and I have no problems using it elsewhere so that's not the problem. The problem is the rngOrderId variable!

    You may well have declared it but there was nothing in your original to indicate that was the case. We're not psychic - well not very often anyway!

  7. #7
    Registered User
    Join Date
    11-27-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: SELECT based on a column in Excel

    Quote Originally Posted by Richard Buttrey View Post
    You may well have declared it but there was nothing in your original to indicate that was the case. We're not psychic - well not very often anyway!
    Absolutely, I should have been more clear!

  8. #8
    Registered User
    Join Date
    11-27-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: SELECT based on a column in Excel

    Quote Originally Posted by Norie View Post
    You'll need to create a comma delimited string from the values in the range rngOrderId and use that in the IN clause of the SQL statement.

    Something like this might work for numeric IDs.
    Please Login or Register  to view this content.

    PS The error was probably because you used Set for the, string, SQL statement, not the variable rngOrderId.
    I Think my problem might be that the rngOrderId uses single quotation marks. Every order number looks like '12345' so that I have a string of
    '12345','12364','12654'
    . I'm fearing that this makes the SQL-string into VBA-comments? How can I get around this? I need the ' around the ordernumbers for the SQL to work.

  9. #9
    Registered User
    Join Date
    11-27-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: SELECT based on a column in Excel

    I found out what the new problem was. I have alot of orders and that makes my sqlstatement longer than the maximum length which is 32.768 characters. I don't seem like I can get around that any way though!

+ 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. [SOLVED] Select column based on column header name
    By D_N_L in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-31-2014, 08:09 AM
  2. Select column based on date
    By miss_gee_gee in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-21-2014, 09:46 PM
  3. [SOLVED] select a value in row based on column value
    By madhatter40 in forum Excel General
    Replies: 1
    Last Post: 06-15-2013, 12:28 PM
  4. [SOLVED] select column based on value in row
    By drfarmkid in forum Excel General
    Replies: 1
    Last Post: 06-01-2012, 06:15 PM
  5. [SOLVED] Select a column based on a index
    By VH in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-06-2006, 05:45 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