2012-01-17 71 views
0

我是編程新手,我試圖通過我的網頁查詢工作。excel vba - 在網絡查詢中創建參數和循環

代碼中,我迄今是這樣的......

Sub webquery1() 

    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "URL;http://dealoz.com/prod.pl?cat_id=&op=buy&op2=&lang=en-us&search_country=us&shipto=us&cur=usd&zip=&nw=y&class=&pqcs=&pkcs=&quantity=&shipping_type=&sort=&catby=&query=[""089203579X""]&ean=[""9780892035793""]&mpn=&asin=&rcount=2" _ 
     , Destination:=Range("A1")) 
     .Name = _ 
     "prod.pl?cat_id=&op=buy&op2=&lang=en-us&search_country=us&shipto=us&cur=usd&zip=&nw=y&class=&pqcs=&pkcs=&quantity=&shipping_type=&sort=&catby=&query=[""089203579X""]&ean=[""9780892035793""]&mpn=&asin=&rcount=2" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .BackgroundQuery = True 
     .RefreshStyle = xlOverwriteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .WebSelectionType = xlSpecifiedTables 
     .WebFormatting = xlWebFormattingNone 
     .WebTables = "10" 
     .WebPreFormattedTextToColumns = True 
     .WebConsecutiveDelimitersAsOne = True 
     .WebSingleBlockTextImport = False 
     .WebDisableDateRecognition = False 
     .WebDisableRedirections = False 
     .Refresh BackgroundQuery:=False 

    End With 

End Sub 

我想參數添加到該網站查詢,例如,該網站將採取ISBN編號從我對另一個Excel列表片。 每個isbn號碼將進入網絡查詢並將信息輸入到單元格中。然後代碼將重複,直到它通過我列表中的所有isbn數字。 感謝您的任何幫助!

回答

1

沒有做這件事,並測試它(不Excel中對我的Ubuntu箱),你可以這樣開始

Sub webquery1(byval isbn10 as string, byval isbn13 as string) 

然後用

& isbn10 & chr(34) & chr(34) & "]&ean=[" & chr(34) & chr(34) & isbn13 & 

可以更換

089203579X""]&ean=[""9780892035793 

不得不平衡所有的字面雙引號和字母(34),但你會到達那裏。然後你需要一個子迭代另一個表中的isbns並調用這個子類。你真的需要兩種類型嗎?他們中的一個有時會失蹤?

Sub iter_thru_isbn_sheet_calling_webquery() 
    dim range_isbns as range 
    dim c as range 
    set range_isbns = sheets("whateveritscalled").range("addressgoeshere") 
    For each c in range_isbns 
     if not c.value = "" then '<---- better way to use regexp but you can research 
      call webquery1(c.value, c.offset(0,1).value) '<---if 10s and 13s side-by-side 
      doevents '<---- query can take awhile. I don't do web queries in vba, so ?? 
     end if 
    next c 
End Sub 

你需要解決如何同時收集ISBN和防止循環訪問,並通過您已經捆綁了它的「孿生」的系列名或10。如果僅僅將詳細信息記錄到文本文件中,還要養成爲每個子程序編寫錯誤處理程序的習慣。對於Excel的東西先去這些傢伙:ozgrid,mrexcel,Pearson。我會張貼鏈接,但現在是時候了。祝你好運。