开始宏录制,进入Data -> From Web,在导入对话框中打开需要的网页,选择需要的表,根据需要设置所有导入选项,导入数据。然后停止录制。
您将获得一个带有自动生成的宏代码的样板,然后将其微调为您的需求将是微不足道的。
我做了很多次,这太容易了,我现在还没有存储一个片段与你分享。
更新:这是如何从这个页面导入你的问题
Sub Macro1()
'
' Macro1 Macro
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://stackoverflow.com/questions/18158928/get-tabledata-from-webpage-into-excel-using-macro/18160278#18160278" _
, Destination:=Range("$A$1"))
.Name = "18160278#18160278"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells ' adjust this setting to your needs
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "1" ' this is the number of the required table on a page
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = True
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub