• Syntax for QueryTable (XL2K)

    Author
    Topic
    #368181

    What is the correct syntax to refresh an external data query? I have tried
    Sheets(“InputData”).QueryTable(“ReportData”).Refresh
    Which gives me an error message .

    Thanks for any help cheers

    Peter

    Viewing 1 reply thread
    Author
    Replies
    • #576193

      Hi Peter, this from one of my workbooks,

      ThisWorkbook.Sheets("LABORGUIDE").QueryTables(1).Refresh
      

      I am not sure that you can refer to the querytable by name. Also, I believe that the sheet will have to be unprotected before it will refresh.

      • #576195

        Thanks for the info smile

        I still have not found a way to refer to the query by name and am not sure how to find the number short of trial and error!
        but have found this method which does what I need.

        Worksheets(“InputData”).Range(“c10”).QueryTable.Refresh

        would prefer to refer to query by name though in case I need to modify things!

        Peter

        • #576203

          This works for me:

          sName=activesheet.querytables(1).name
          activesheet.querytables(sName).refresh

        • #576251

          You can index the QueryTables collection by name. As Rory pointed out, there is the QueryTables collection as opposed to the QueryTable property of a Range. You can determine the name by

          Sheets("InputData").Range("C10").QueryTable.Name

          If you used Excel to create the query, then Excel uses the Name “Query from XXXX”, where XXXX is the DataSource, so maybe Sheets(“InputData”).QueryTables(“Query from ReportData”).Refresh is what you need. You can also deternine the Query name from the Name box (to the left of the Formula bar). If you have the query with the name “Query from XXXX”, then you will have the name Query_from_XXXX in the drop-down. If you used VBA to create the query, then you should use the Name property to give it a useful name. HTH –Sam

          • #576355

            Many Thanks for all of the replies smile

            Rory had it exactly right! Surprising how much difference a little old “s” can make! Always used to be the punctuation that had me pulling my hair out hairout

            I was able to get the query name from the properties sheet for it on the External Data Tool Bar

            Cheers all cheers

    • #576232

      Hi Peter,
      I suspect you need to change
      Sheets(“InputData”).QueryTable(“ReportData”).Refresh
      to read
      Sheets(“InputData”).QueryTables(“ReportData”).Refresh
      Hope that helps.

    Viewing 1 reply thread
    Reply To: Syntax for QueryTable (XL2K)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: