+ Reply to Thread
Results 1 to 10 of 10

Queries in code

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Queries in code

    I am trying to write a query in VBA but have fallen at the last fence

    Please Login or Register  to view this content.
    This falls down at the last two lines marked with* where I get "Method Range of Object_Global failed". I have tried avoiding the Select and using the variable Dest but to no avail
    Can anyone see where I have gone wrong. I have not given the details of the SQLstr, but could it be there ? Or is it to do with using Select
    Last edited by j_Southern; 03-23-2012 at 12:35 PM.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Queries in code

    You need to reference a specific named ListObject, an instance of the ListObjects collection.

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Queries in code

    Bob
    I am not sure what a ListObject is, they only seem to have become neccessary since Excel2007. Certainly there will only be one object at the range specified. There is only one QueryTable on each sheet anyway.
    Should I run something like
    For Every ListObject in ListObjects

    to find what I am looking for
    John

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Queries in code

    John, in Excel 2007 and 2010, a listobject is an Excel table. If you select inside your table, and then pick the Design tab from the Tables contextual ribbon tab, you will see the table name in the far left, where you can change it. It will likely be Table1 if you haven't changed it. You code would then be

    Please Login or Register  to view this content.
    (I think, I haven't actually tested it).

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Queries in code

    Bob
    Thanks for that. I cant check at the moment, but I think there will be a problem. This is the first instance of the query, so there won't be a table until the refresh has been carried out!
    I will try it out in the morning and let you know what happens.
    John

  6. #6
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Queries in code

    Bob,
    I have tried your suggestion and I get error "Object does not support method or property" or "Wrong number of arguments or invalid property assignment" depending on whether I use Dest as range or type it out fully as you have.
    I am looking at the SQL string now in case this is the difficulty.
    John

  7. #7
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Queries in code

    You don't show the SQL so I cannot comment, maybe a workbook and the data source would help.

  8. #8
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Queries in code

    Bob
    The data source is my companies Sage accounting system so its not really possible to send it. I could pull part of two of the files I am using into two worksheets, but I dont think you can use an ODBC link from Excel to Excel. If that would help let me know
    Any way here is the Function I use to generate the SQL string.
    Please Login or Register  to view this content.
    During my testing I had Remd out everything except SELECT, the first field, FROM one table, and I still got the error.
    The parameter StartD is not used in this SQL string, but it is in others as a criterion to set the earliest WM.INVOICE_DATE
    John
    Last edited by j_Southern; 03-23-2012 at 11:15 AM. Reason: remove typo

  9. #9
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Queries in code

    Bob and others
    I Have found the problem.
    The replacement of
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    did the trick. I realised that the first bit of code was used to refresh an existing query.
    Thanks for your help anyway. I can mark the thread solved
    John

  10. #10
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Queries in code

    You could always take the lump hammer approach

    ActiveWorkbook.RefreshAll

+ 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