VBA Recorded Macro Web Query for Data, how to repeat the query for next item on the list

BennyBB

New Member
Joined
Sep 6, 2013
Messages
33
On sheet1 in Excel 2007 I have a list starting in cell A1=cat, B1=dog, C1=mouse.

I recorded a macro that does a web query for data at Yahoo and for search term "cat".

How can I repeat the macro for the whole list A1, B1, C1?

Code:
Sub YahooSearch()
'
' YahooSearch Macro
'

'
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://search.yahoo.com/search;_ylt=A2KK_foKYzdS7TQAH36bvZx4?p=cat&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900" _
        , Destination:=Range("Sheet2!$A$1"))
        .Name = _
        "search;_ylt=A2KK_foKYzdS7TQAH36bvZx4?p=cat&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900"
        .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
    Sheets("Sheet2").Select
    ActiveWindow.SmallScroll Down:=159
End Sub

Thanks,
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try:
Code:
Sub YahooSearch()
    Dim i As Integer, n As Integer, SearchString As String, ws As Worksheet
    Set ws = Sheets("Sheet1")
    n = ws.Range("A1").End(xlToRight).Column
    For i = 1 To n
        SearchString = Cells(1, i)
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;http://search.yahoo.com/search;_ylt=A2KK_foKYzdS7TQAH36bvZx4?p=" & SearchString & "&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900" _
            , Destination:=Range("Sheet2!$A$1"))
            .Name = _
            "search;_ylt=A2KK_foKYzdS7TQAH36bvZx4?p=" & SearchString & "&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900"
            .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
        Sheets("Sheet2").Select
        ActiveWindow.SmallScroll Down:=159
        MsgBox "Hit OK when you wish to proceed to the next search item.", vbOKOnly, "Next Search"
    Next i
End Sub
I haven't tested it in full so make sure to run it in a copy of your workbook just in case. That said, I did the basic concept of selecting A1 and working right through the columns and building the URLs from that and it worked fine. The code grabs the string from the cell and then concatenates it in to the search URL.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top