我需要幫助,我的代碼不會將數據拉到它從網站創建的新工作表中。它出現爲空白。這真的令人沮喪。在我將字符串變量「縣」指定爲網站地址後,查詢表將不會提取數據。我看遍了互聯網,並沒有找到如何解決這個問題的答案。VBA查詢表不會將數據拉入到它從網站創建的新工作表中,它將不會將數據拉到工作表中。
counties = Range(「HTML」)。Offset(x,0)顯示等於08/08001.html,它是網站地址的一部分。
Sub Macro6()
Dim x As Integer
Dim counties As String
For x = 1 To 3
Sheets("RawData").Select
counties = Range("HTML").Offset(x, 0)
Sheets.Add.Name = "DataTemp"
With ActiveSheet.QueryTables.Add(Connection:="URL;http://quickfacts.census.gov/qfd/states/" & counties & ".html", Destination:=Range("$A$1"))
.Name = "08001"
.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"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
End With
'這部分將數據從新創建的「DataTemp」工作表移動到「Demographics」工作表中。
Columns("A:B").Select
ActiveWindow.ScrollColumn = 2
Range("A:B,D:D").Select
Range("D1").Activate
Selection.ClearContents
Range("C1:C63").Select
Selection.Copy
Sheets("Demographics").Select
Cells(6, x + 2).Select
ActiveSheet.Paste
Columns("C:C").EntireColumn.AutoFit
ActiveSheet.Previous.Select
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
下一個x
末次
除了關閉For ... Next語句之外,您正在循環三次,但每次reDim字符串變量並且每個新查詢表都被賦予相同的名稱。您還可以編輯您的問題,以提供「ActiveSheet.Cells(x,1).Value」中找到的示例。 – Jeeped
好的,我對代碼做了一些修改,現在怎麼樣?即使對於第一個值,查詢表也不會在名爲「DataTemp」的新工作表中顯示值。 –
我正在嘗試代碼清理,但傳輸數據的記錄代碼的後一部分與記錄的所有命令混淆,但實際上沒有任何用途。你能提供一個描述你在那裏做什麼的敘述嗎? – Jeeped