2017-02-04 99 views
0

我試圖下載馬匹的數據表。用列中的值替換部分URL

宏中唯一的變量是URL的一部分。

我有列在「A」列出的所有變量(馬編號)。

我想使用從網站收集的信息創建一個新工作表,然後使用列「A」中下一行的數字(新變量)再次收集信息。

到目前爲止,這是我的代碼:

Sub Makro1() 
' 
' Makro1 Makro 
' 
Dim nummer As String 

nummer = Sheets("Ark1").Range("A1:A10") 

' 
    ActiveWorkbook.Worksheets.Add 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "URL;http://195.198.34.45/trav/hast/visa/" & nummer & "/resultat", Destination:=Range _ 
     ("$A$1")) 
     .CommandType = 0 
     .Name = "resultat" 
     .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 = True 
     .WebDisableRedirections = False 
     .Refresh BackgroundQuery:=False 
    End With 
    Columns("D:D").Select 
    ActiveWindow.SmallScroll Down:=21 
    Columns("E:E").Select 
    Range("E22").Activate 
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
    Columns("D:D").Select 
    Range("D22").Activate 
    Selection.TextToColumns Destination:=Range("D1"), DataType:=xlDelimited, _ 
     TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=True, Semicolon _ 
     :=False, Comma:=False, Space:=False, Other:=True, OtherChar:="/", _ 
     FieldInfo:=Array(1, 2), TrailingMinusNumbers:=True 
End Sub 

User sheet trying to describe problem

+0

我和你在一起,直到「然後在coulmn的下一行做」A「」 - 你究竟是什麼意思? – TheSilkCode

+0

我想我試圖創建一個多重查詢。 多重查詢的所有變量均以coulmn列出A –

回答

0

如何處理多個查詢,不知道你想和每一個結果

Sub Makro1() 
' 
' Makro1 Makro 
' 
    Set ws = ActiveSheet 'sheet containing your numbers in column A 
    ActiveWorkbook.Worksheets.Add 
    For Each nummer In ws.Range("A1:A10") 
    If nummer.Text = "" Then Exit For 
     For Each qt In ActiveSheet.QueryTables 
      qt.Delete 
     Next 
     With ActiveSheet.QueryTables.Add(Connection:= _ 
      "URL;http://195.198.34.45/trav/hast/visa/" & nummer.Text & "/resultat", Destination:=Range("$A$1")) 
      .Name = "resultat" 
      .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 = True 
      .WebDisableRedirections = False 
      .Refresh BackgroundQuery:=False 
      nummer.Offset(0, 1) = ActiveSheet.Cells(21, 1) 
     End With 
    Next 
    Application.DisplayAlerts = False 
    ActiveSheet.Delete 
    Application.DisplayAlerts = True 
End Sub 
做一個命題
+0

感謝您的幫助。 有一個問題,與第二和第三查詢我想.. 從我的理解這一點: 'Set WS = ActiveSheet' 意味着它將使用活動板在接下來的查詢,但我想它使用我的「主」表的下一行中的數字。 –

+0

我試圖添加一些到我原來的文章,並添加了一張圖片。 我會稍後使用收集的數據進行進一步處理 謝謝 –

+0

我已編輯代碼,將名稱發現旁邊的號碼 – h2so4