Link to home
Start Free TrialLog in
Avatar of Schandor
Schandor

asked on

Excel Web Query Macro

Hi all,
I am extremely new to VB so please bear with me. I am trying to write a macro for Excel that will query a website and then pull in some data.  I have been able to get it to work via prompting the user for input.  However, I would rather the user simply fill in some cells/fields in the worksheet and use that data for the query.   Does anyone know how to specify a particular cell in VB?  In particular doing a wbe query?

Here is what I have in the webquery that work with prompting:

http://www.watkins.com/autosub/autorate.asp?org=["OriginZip"]&dest=["DestZip"]&weight1=["Weight1"]&class1=["Class1"]

The macro prompts me for data between the "[" and "]" symbols.  If you have any questions let me know and I would be happy to clarify.

Thank you for your help.
Avatar of Richard_Eustace
Richard_Eustace

are you using VB or VBA (the microsoft office scripting language?

If it is VBA than to specify a particulat cell use

Sheets("insert sheet name here in quotes").Cells(1, 1).Select

or

Sheets("insert sheet name here in quotes").Range("E1").Select



Hello Schandor,

why not build the string url in a cell E1 like

="http://www.watkins.com/autosub/autorate.asp?org="&A1&"&dest="&B1&"&weight1="&C1&"&class1="&D1

A1, B1, C1, D1 are the cells the user fills for the formula in E1

then you can use this as the url in your webquery you can build in code

if you need more let me know, got no code at hand only this PAQ which contains everything you need :)

https://www.experts-exchange.com/questions/20294436/Excel-function-Template-to-create-yearly-average-of-monthly-stock-averages-for-a-Series-of-downlaoded-stocks-Which-service.html

HAGD:O)Bruintje
Avatar of Schandor

ASKER

Hi all,
Sorry for the delay in getting back, work fires abound.  Richard, I must be using VBA as I am on a Mac and using Office X's editor. Sorry for the confusion there.

bruintje, I like your idea, but I am unsure of how to take a URL sitting in a cell and use it in a Web query that changes dynamically.  I may be blind but I am having a heck of a time finding documentaion on using Web query.  Your thoughts?
in that link above you can download a workbook called yahloader.xls everything is in there
bit short on time now but post if something is not clear
Hi all,
OK, I gave that a try and wrote this:  

Sub Shipping_Rates()
'
' Shipping_Rates Macro
'
Dim strURL
'
    strURL = Worksheets("Sheet1").Range("A1")
    Range("A10").Select
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;" & strURL _
        .PostText = "test"
        .Name = False
        .FieldNames = False
        .RefreshStyle = xlInsertDeleteCells
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .HasAutoFormat = True
        .RefreshOnFileOpen = 1
        .BackgroundQuery = False
        .TablesOnlyFromHTML = True
        .SaveData = True
        .Refresh BackgroundQuery:=False
        .UseListObject = False
    End With
End Sub

Now I am getting compile errors.  The URL in the worksheet is correct but I'm still having trouble referencing it.  The URL is in the worksheet "Sheet1" in cell A1.  I was hoping to stick the site's contents in cell A10.  Thoughts?  

By the way, thank you for all your help.
Why don't you record a new macro, and use the New Web Query to generate the code for you ( can be found here - Data->Import External Data->New Web Query).  You can then see how the code should look and than you can tailor it to suit your needs.

Hi Richard,
Actually, that's what I tried (the recording, that is).  My tailoring isn't quite up to snuff is the problem.

Thank you.
What URL are you trying to Access?, What sort of data are you trying to retrieve?
ASKER CERTIFIED SOLUTION
Avatar of Richard_Eustace
Richard_Eustace

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Richard,
OK, your first method seems to be what i want to do, but I get a syntax error when I compile it (via a copy and paste).  Sorry to be such a pest.
What is the error?

did you set the sheet up like this

'Spreadsheet layout
'       A       B       C       D
'1      ORG     dest    weight1 class1
'2      55368   55344   2500    125
Yes it is set up that way.  As far as the error, all it tells me is:

Comile Error:  Syntax Error

It then has the two DIM statements highlited in red as well as the URL statement.  Due to my lack of knowledge and your patience, I'm increasing the points on this one.  Thanks for the help.
What version of Excel are you running. I don't think it should mater much But just checking.  I am running the version which comes with Office XP. I cannot think why you are getting an error. I copied the cection of code out of this toppic and pasted it into excel and it works fine.
Microsoft Excel X for Mac, Service Pack 1 is installed
I don't know about excel for Macs.  I therefore assume the syntax is slightly different.

Sorry I cannot help
Still haven't gotten this to work completely but it has since become a back-burner issue.  Hence, I will award your points, apologize for taking so long to get back, and thank you for your help

Have a nice day