VBA For Loop将数据刮入新工作表

时间:2014-11-11 18:41:54

标签: excel vba loops excel-vba for-loop

我正在excel中进行一些数据抓取,并尝试使用以下代码将数据粘贴到新的工作表中:

Sub ZillowScrape()
'
' ZillowScrape Macro
'
' Keyboard Shortcut: Ctrl+z


    For x = 1 To 5
    Worksheets("URLs").Select
    Worksheets("URLs").Activate
    mystr = "URL;http://www.zillow.com/homedetails/332-Blanca-Ave-Tampa-FL-33606/45111195_zpid/"
    mystr = Cells(x, 1)

    ThisWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = x

   With ActiveSheet.QueryTables.Add(Connection:=mystr, Destination:=Range("$A$2"))
    'CommandType = 0
    .Name = "01000_1"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingNone
    .WebTables = "3,4,5"  '---> Note: many tables have been selected for import from the website
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With
Next x
End Sub

我一直收到错误"运行时错误' 1004':应用程序定义的错误或对象定义错误"换行:

With ActiveSheet.QueryTables.Add(Connection:=mystr, Destination:=Range("$A$2"))

1 个答案:

答案 0 :(得分:0)

我认为问题在于此代码:

mystr = "URL;http://www.zillow.com/homedetails/332-Blanca-Ave-Tampa-FL-33606/45111195_zpid/"
mystr = Cells(x, 1)

我认为解决方案是:

 mystr = "URL;http://www.zillow.com/homedetails/332-Blanca-Ave-Tampa-FL-33606/45111195_zpid/"+Cells(x,1).value