標題:
[發問]
第一次學用VBA抓取資料 遇到一些問題
[打印本頁]
作者:
s93213241
時間:
2013-12-19 01:18
標題:
第一次學用VBA抓取資料 遇到一些問題
前言: 小弟我有試著用簡單的iqy去撰寫也成功了,但只要匯入4個以上(含),程式就會跑出無法抓取web資料沒辦法只好再來自學vba
以下是我爬了3天的成果 以下程式碼都是用錄製取的 總共有5個動作需要做 前面四個抓出來都是亂碼但是重要的數值都有截取出來,但是他都會在插入行在運算結果 很困擾我
最後一段程式碼想寫出只顯示出當日表單 只要修改紅色日期相對就會帶出那個日期的表單
程式希望可以在a分頁執行 在b分頁帶出結果出來
以上感謝看完此篇的有緣人 謝謝 假如有其他需要我補充說明的歡迎提出 因為並不能直接開網頁出來看結果
Sub 巨集1()
'
' 巨集1 巨集
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://cagweb02.hct.com.tw/pls/cagweb/CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=03897810001" _
, Destination:=Range("$A$1"))
.Name = _
"CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=03897810001"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://cagweb02.hct.com.tw/pls/cagweb/CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=04413850008" _
, Destination:=Range("$A$7"))
.Name = _
"CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=04413850008"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://cagweb02.hct.com.tw/pls/cagweb/CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=03909690001" _
, Destination:=Range("$A$13"))
.Name = _
"CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=03909690001"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://cagweb02.hct.com.tw/pls/cagweb/CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=02074160008" _
, Destination:=Range("$A$19"))
.Name = _
"CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=02074160008"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Dim URL As String, xDate As Date
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://hct-apdev1.hct.com.tw:11235/HCT.AS400.Web/CargoTracking/CTGPS01R2.aspx?ScanDay=[color=Red]20131218[/color]&Stor=6006&StorDesc=%e5%8f%b0%e5%8d%97&CseNo=All&OpeCd=All" _
, Destination:=Range("$A$1"))
.Name = _
"CTGPS01R2.aspx?ScanDay=[color=Red]20131218[/color]&Stor=6006&StorDesc=%e5%8f%b0%e5%8d%97&CseNo=All&OpeCd=All"
.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 = "1"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
複製代碼
作者:
s93213241
時間:
2013-12-21 13:52
本帖最後由 s93213241 於 2013-12-21 13:53 編輯
以自行解決
作者:
handsometrowa
時間:
2013-12-24 09:14
回復
2#
s93213241
其實有看沒有懂
請問版大可否上傳檔案以茲大家學習領悟呢??
作者:
s93213241
時間:
2013-12-24 14:22
本帖最後由 s93213241 於 2013-12-24 14:24 編輯
我檔案分享出來也沒有用@@ 因為我在PO文一開始就說明 我公司室內網 一般民網連不上去
Dim xDate As Date
xDate = Date
Range("Sheet2!$E$6").Value = (xDate)
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://hct-apdev1.hct.com.tw:11235/HCT.AS400.Web/CargoTracking/CTGPS01R2.aspx?ScanDay=" & Format(xDate, "YYYYMMDD") & "&Stor=6006&StorDesc=%e5%8f%b0%e5%8d%97&CseNo=All&OpeCd=All" _
, Destination:=Range("Sheet1!$A$25"))
.Name = _
"CTGPS01R2.aspx?ScanDay=" & Format(xDate, "YYYYMMDD") & "&Stor=6006&StorDesc=%e5%8f%b0%e5%8d%97&CseNo=All&OpeCd=All"
.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 = "1"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
複製代碼
以上這段我卡住比較久
" & Format(xDate, "YYYYMMDD") & " 用法
與
Dim xDate As Date
xDate = Date
宣告當日時間
剩下登入畫面我上網找人家PO出來的成品自己修改進去的
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)