+ Reply to Thread
Results 1 to 14 of 14

Using web query with parameters within a macro

  1. #1
    Registered User
    Join Date
    05-19-2017
    Location
    Boston, MA
    MS-Off Ver
    2010
    Posts
    9

    Using web query with parameters within a macro

    I am trying to create a macro that will run a web query that automatically references a specific cell to input the parameter, but I keep getting the prompt to input the parameter when I run the macro (URL changed for privacy).

    With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://webquery.com/profile.[""ID""]", _
    Destination:=Range("A3"))
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With
    With ActiveWorkbook.Connections("Connection")
    .Name = "Connection"
    .Description = ""

    I should say that I am not great with code, so I'm mostly trying to do this by recording, which is likely where I'm going wrong. Thanks in advance for your help!

  2. #2
    Registered User
    Join Date
    05-19-2017
    Location
    Boston, MA
    MS-Off Ver
    2010
    Posts
    9

    Re: Using web query with parameters within a macro

    Bumping in case anyone can help with this!

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using web query with parameters within a macro

    Where exactly do you want to put in the parameters? It is not clear from the URL.

    "URL;http://webquery.com/profile.[""ID""]"

    If it is after profile, it might be

    "URL;http://webquery.com/profile/"& cells(1,1)
    Where cells(1,1) is the argument which comes from cell 1,1. You can substitute it with a variable cell address. Let's say you have a loop. cells(I,1) where I is the loop variable.

  4. #4
    Registered User
    Join Date
    05-19-2017
    Location
    Boston, MA
    MS-Off Ver
    2010
    Posts
    9

    Re: Using web query with parameters within a macro

    Yes, I want to put the parameters in place of "ID". Ideally, I'd also like it to update if that number is changed. So if the parameter is located in cell B1, how would I write that?

    Thank you!

  5. #5
    Registered User
    Join Date
    05-19-2017
    Location
    Boston, MA
    MS-Off Ver
    2010
    Posts
    9

    Re: Using web query with parameters within a macro

    Alternatively, I came up with this, which seems to work but doesn't update when O2 is changed. Is there a way to tell it to do that?

    With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;" & Sheets("Worksheet").Range("O2").Value, Destination:=Range("$A$3"))
    .Name = "profile.cfm?authid=[""ID""]_1"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With
    End Sub

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using web query with parameters within a macro

    I am assuming you have values in column B2 and down

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-19-2017
    Location
    Boston, MA
    MS-Off Ver
    2010
    Posts
    9

    Re: Using web query with parameters within a macro

    That did it! Thank you so much!

  8. #8
    Registered User
    Join Date
    05-19-2017
    Location
    Boston, MA
    MS-Off Ver
    2010
    Posts
    9

    Re: Using web query with parameters within a macro

    OK, that is mostly working but some of the web queries are coming back with "Error: Condition Failed" even though there is a site with those parameters and if I do an individual web query for that parameter, it works. Do you know why that might be?

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using web query with parameters within a macro

    Difficult to comment with out seeing the error on the actual page. Could you post the URL with the parameter which errors?

  10. #10
    Registered User
    Join Date
    05-19-2017
    Location
    Boston, MA
    MS-Off Ver
    2010
    Posts
    9

    Re: Using web query with parameters within a macro

    I can't share the URL, but I can show you the error message. I was actually wrong, and even doing the full URL individually doesn't work with a web query, even though the web page definitely exists. This is what comes up:

    Error.png
    Last edited by Makalaja; 05-24-2017 at 12:55 PM. Reason: Incorrect info

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using web query with parameters within a macro

    IE automation does not understand manual interference, hence the error. I do not think there is a round way. You can put in an error trap in the code, but it merely hides the error; that is, the code will not stop and will carry on the next loop, but you will get a zero result for that row.

    Put this line
    on error resume next

  12. #12
    Registered User
    Join Date
    05-19-2017
    Location
    Boston, MA
    MS-Off Ver
    2010
    Posts
    9

    Re: Using web query with parameters within a macro

    Would using a different browser work? And if so, how would I tell it to do that? :-)

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Using web query with parameters within a macro

    I think Power query works well with IE. You could try if you have excel 2013, 2016 or office 365.

  14. #14
    Registered User
    Join Date
    05-19-2017
    Location
    Boston, MA
    MS-Off Ver
    2010
    Posts
    9

    Re: Using web query with parameters within a macro

    I'll look into that, thank you!

+ 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. SQL Query and parameters
    By springwood928 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2015, 04:27 PM
  2. automatic Web Query with different parameters
    By mschatz2 in forum Excel General
    Replies: 15
    Last Post: 08-23-2011, 12:20 PM
  3. MS Query - Parameters
    By mattydalton in forum Excel General
    Replies: 4
    Last Post: 11-17-2009, 11:48 AM
  4. Web Query w/parameters
    By sarnone in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-23-2009, 02:01 PM
  5. Query parameters
    By Lac in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2006, 08:10 AM
  6. [SOLVED] Query Parameters
    By Texas_Toast in forum Excel General
    Replies: 0
    Last Post: 01-11-2006, 11:20 AM
  7. [SOLVED] Parameters for Query
    By Sujith in forum Excel General
    Replies: 1
    Last Post: 04-03-2005, 05:07 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