+ Reply to Thread
Results 1 to 13 of 13

How to highlight a row based on a DB query result?

  1. #1
    Registered User
    Join Date
    09-25-2014
    Location
    Utah
    MS-Off Ver
    2010 and 2013
    Posts
    8

    How to highlight a row based on a DB query result?

    I am new to programming Excel and could use some help. I have read a few thing but not able to piece it together.

    I have a worksheet that has a list of values, say in Column A. I want to query a SQL database to see if that value exists in the database and if it does highlight the row for each row. Ideally it would do this after entering the number and moving to the next row. I could really use the help as I am at a loss on where to go. Do I do this in VBA, macro, ...?

    My sql statement in a macro looks like this.

    Please Login or Register  to view this content.
    This is as far as I have been able to get to and not sure where to go from here. From what I understand this part works fine in getting the information from the DB, just not sure how to highlight the rows that have a returned value.

    Example:

    If A had the following
    1154
    1155
    1156
    1157

    The query returns a 1 for 1154 and 1157 and NULL for 1155 and 1156. How would I now go about highlighting 1154 and 1157? I figure I could have the results enter the 1 in the given row in column B and have a highlighting rule but not sure how to do that either.

    I tried
    Please Login or Register  to view this content.
    but it just enters the results to Row 2 starting in column B.

    Hope this all makes sense.
    Last edited by omakler; 09-25-2014 at 07:05 PM.

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

    Re: How to highlight a row based on a DB query result?

    I think you'd be better off with:
    PHP Code: 
        SELECT x
        FROM 
            table
        WHERE
            x in 
    (11541155,1156,1157
    This would return a recordset of simply the invoice ids(?) that are in the database. You can then just do a VLookup on your primary dataset. You only then need to do 1 query

  3. #3
    Registered User
    Join Date
    09-25-2014
    Location
    Utah
    MS-Off Ver
    2010 and 2013
    Posts
    8

    Re: How to highlight a row based on a DB query result?

    Quote Originally Posted by Kyle123 View Post
    I think you'd be better off with:
    PHP Code: 
        SELECT x
        FROM 
            table
        WHERE
            x in 
    (11541155,1156,1157
    This would return a recordset of simply the invoice ids(?) that are in the database. You can then just do a VLookup on your primary dataset. You only then need to do 1 query
    I agree that would be a possibility and less queries, but as I mentioned I am stuck at the aspect as well. I can get the results to print to the ActiveSheet but it is in 1 row not along a column, which would allow me to do a vLookup.

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

    Re: How to highlight a row based on a DB query result?

    That query should give you a list of numbers in a single column

    Vlookup works down a column , just return the first column. You could use match if you prefer though

  5. #5
    Registered User
    Join Date
    09-25-2014
    Location
    Utah
    MS-Off Ver
    2010 and 2013
    Posts
    8

    Re: How to highlight a row based on a DB query result?

    excelresult.png
    You would think so yes, but it returns the results into a row not column as seen above.

  6. #6
    Registered User
    Join Date
    09-25-2014
    Location
    Utah
    MS-Off Ver
    2010 and 2013
    Posts
    8

    Re: How to highlight a row based on a DB query result?

    Please Login or Register  to view this content.
    Here is the code currently which gives the previous post result.

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

    Re: How to highlight a row based on a DB query result?

    Of course it does because you're only returning a single record?

    Fields go across, records go down the sheet. Each time you are running the query it inserts a cell with the value in and moves the previous value across (I'm guessing)

    Try my way - you will end up with a list of numbers going straight down column B and only need to query the db once

  8. #8
    Registered User
    Join Date
    09-25-2014
    Location
    Utah
    MS-Off Ver
    2010 and 2013
    Posts
    8

    Re: How to highlight a row based on a DB query result?

    How would I go about adding all row.values into the SQL? The numbers of values that I would need to add to the query could range from a few dozen to several hundred.

    THANK YOU for helping out. I don't do this normally so all pointers are appreciated

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

    Re: How to highlight a row based on a DB query result?

    No problem at all

    All you need to do is keep adding the ids to the SQL string you build in your loop with commas between them. Once the loop has finished pass the SQL string to the query table.

    Try a hard coded SQL string first to see if it gives you the result you expect, then incorporate the loop afterwards.

  10. #10
    Registered User
    Join Date
    09-25-2014
    Location
    Utah
    MS-Off Ver
    2010 and 2013
    Posts
    8

    Re: How to highlight a row based on a DB query result?

    Ok I go that working
    Please Login or Register  to view this content.
    I tis returning the resultset to a new sheet starting in A1 and that is great. Now here is the caveat. The actual file may have the same number but a different value in column B and person in column C. How would I add this to the SQL so that it would look like
    Please Login or Register  to view this content.
    Then return that to the sheet correctly.

    The whole story is I have a list of invoices from vendors that I want to check against the database to see if I have already entered a similar invoice in the system before uploading a potential duplicate. Again THANKS!!!!!

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

    Re: How to highlight a row based on a DB query result?

    I wouldn't, I'd just return all those columns and leave the query as it is. You can then do the matching in Excel, it simplifies things somewhat

  12. #12
    Registered User
    Join Date
    09-25-2014
    Location
    Utah
    MS-Off Ver
    2010 and 2013
    Posts
    8

    Re: How to highlight a row based on a DB query result?

    I haven't looked yet but is there a way to embed the DSN connection and not allowing a user to access the information of the connection for viewing without a password?

  13. #13
    Registered User
    Join Date
    09-25-2014
    Location
    Utah
    MS-Off Ver
    2010 and 2013
    Posts
    8

    Re: How to highlight a row based on a DB query result?

    Thanks for the help I think I have what I need and will just allow the connection to be how it is with a DSN file.

+ 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. Check a webpage for specific text and highlight a cell based on result
    By ks100 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-12-2013, 09:56 AM
  2. Web query and query by VBA produce different result than URL in browser
    By 6StringJazzer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-09-2013, 03:43 PM
  3. [SOLVED] Highlight result cells
    By Alac61 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-16-2013, 02:19 AM
  4. Replies: 0
    Last Post: 03-15-2006, 09:40 PM
  5. [SOLVED] vlookup based on random result returns incorrect result
    By rickat in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-05-2005, 09:20 AM

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