+ Reply to Thread
Results 1 to 12 of 12

Macro displays incorrect data when using IN statement in VBA code

  1. #1
    Registered User
    Join Date
    04-06-2016
    Location
    New York
    MS-Off Ver
    Office 2010
    Posts
    12

    Macro displays incorrect data when using IN statement in VBA code

    I've written a macro that takes cell value from excel match it with the sql server table and return the data on the same spreadsheet. I think I'm doing it wrong. I'm not able get accurate data using below piece of code. Instead of using SQL IN statement in my query, I want to match single cell at one time using below SQL statement:

    Please Login or Register  to view this content.

    Here is the entire code for macro:

    Please Login or Register  to view this content.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro displays incorrect data when using IN statement in VBA code

    It's probably not because it's not working, but used wrongly. The 'IN' condition part should be placed after 'WHERE' clause, not under the conditions for 'LEFT JOIN'.
    多么想要告诉你 我好喜欢你

  3. #3
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Macro displays incorrect data when using IN statement in VBA code

    Hi,
    I noticed this post has had a lot of views without gaining a response.

    it would help if you stated what the nature of the inaccuracy is.
    Also to give an example of the contents of the rs on completion of the exec.

    Also I am not sure what you mean by " Instead of using SQL IN statement in my query, I want to match single cell at one time using below SQL statement:"


    click on the * Add Reputation if this was useful or entertaining.

  4. #4
    Registered User
    Join Date
    04-06-2016
    Location
    New York
    MS-Off Ver
    Office 2010
    Posts
    12

    Re: Macro displays incorrect data when using IN statement in VBA code

    millz- You're right. Can you give me a hand on this?

    Tony h- I appreciate your response. In my below condition the s.[ShipTracking] can be same for multiple excel rows. In that situation IN statement doesn't work. Any help is appreciated. Thanks.

    Please Login or Register  to view this content.

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro displays incorrect data when using IN statement in VBA code

    I assume the SELECT portion is working fine.
    Moved the 'IN' segment to 'WHERE' segment.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    04-06-2016
    Location
    New York
    MS-Off Ver
    Office 2010
    Posts
    12

    Re: Macro displays incorrect data when using IN statement in VBA code

    I tried modifying my sql query as you suggested. I'm not getting the desired output. Is there a way to check each record at a time from the spreadsheet. I'm sorry if I'm asking too many questions. I'm new to VBA and stuck with this issue from past 4 days. Thanks for your help.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Macro displays incorrect data when using IN statement in VBA code

    what are the values of newrow2 and newrow3?
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  8. #8
    Registered User
    Join Date
    04-06-2016
    Location
    New York
    MS-Off Ver
    Office 2010
    Posts
    12

    Re: Macro displays incorrect data when using IN statement in VBA code

    Hi Joseph,

    I've attached the screenshot of my excel spreadsheet. newrow2 has column F values and newrow3 has concatenated values from columns D,C, B, a from the spreadsheet.

    Here is the code from newrow2 and newrow3:

    Please Login or Register  to view this content.

    Attachment 455708

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Macro displays incorrect data when using IN statement in VBA code

    I can't open your attachment so can you explain exactly what is wrong with what happens when you run your code now?

  10. #10
    Registered User
    Join Date
    04-06-2016
    Location
    New York
    MS-Off Ver
    Office 2010
    Posts
    12

    Re: Macro displays incorrect data when using IN statement in VBA code

    The newrow3 can have same column values. In that situation my macro IN statement doesn't work correctly. Hope that helps. Here is the example:

    3/23/2016 - 507 - CPT - Group C
    3/23/2016 - 507 - CPT - Group C
    3/23/2016 - 5051 - Flats - Group F

  11. #11
    Registered User
    Join Date
    04-06-2016
    Location
    New York
    MS-Off Ver
    Office 2010
    Posts
    12

    Re: Macro displays incorrect data when using IN statement in VBA code

    The newrow3 can have same column values. In that situation my macro IN statement doesn't work correctly. Hope that helps. Here is the example:

    3/23/2016 - 507 - CPT - Group C
    3/23/2016 - 507 - CPT - Group C
    3/23/2016 - 5051 - Flats - Group F

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Macro displays incorrect data when using IN statement in VBA code

    you can get a unique list like this
    Please Login or Register  to view this content.

+ 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. Copy cell copies correct formula, but displays incorrect values
    By willzzzzzzzz in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-10-2016, 05:28 PM
  2. [SOLVED] VBA code, Pop window displays if macro doesn't find requested condition
    By Tapyr in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-19-2015, 01:30 PM
  3. User Defined Function with If statement, does not work, incorrect code?
    By forrestgump1980 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-07-2014, 09:31 AM
  4. vba help - code pulling incorrect date range from data set
    By vagelisgeo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-24-2013, 02:12 PM
  5. [SOLVED] PivotTable - Code returns incorrect values when using a data connecton
    By efernandes67 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-15-2013, 02:13 PM
  6. conditional formatting displays incorrect value when time is 12:00
    By moonbecky in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-28-2008, 12:44 PM
  7. [SOLVED] Sum Function sometimes displays incorrect answer
    By John Westgate in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-18-2005, 09: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