Board logo

標題: [發問] 第一次學用VBA抓取資料 遇到一些問題 [打印本頁]

作者: s93213241    時間: 2013-12-19 01:18     標題: 第一次學用VBA抓取資料 遇到一些問題

前言: 小弟我有試著用簡單的iqy去撰寫也成功了,但只要匯入4個以上(含),程式就會跑出無法抓取web資料沒辦法只好再來自學vba

以下是我爬了3天的成果  以下程式碼都是用錄製取的 總共有5個動作需要做  前面四個抓出來都是亂碼但是重要的數值都有截取出來,但是他都會在插入行在運算結果 很困擾我

最後一段程式碼想寫出只顯示出當日表單 只要修改紅色日期相對就會帶出那個日期的表單

程式希望可以在a分頁執行 在b分頁帶出結果出來  

以上感謝看完此篇的有緣人 謝謝   假如有其他需要我補充說明的歡迎提出  因為並不能直接開網頁出來看結果
  1. Sub 巨集1()
  2. '
  3. ' 巨集1 巨集
  4. '

  5. '
  6.     With ActiveSheet.QueryTables.Add(Connection:= _
  7.         "URL;http://cagweb02.hct.com.tw/pls/cagweb/CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=03897810001" _
  8.         , Destination:=Range("$A$1"))
  9.         .Name = _
  10.         "CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=03897810001"
  11.         .FieldNames = True
  12.         .RowNumbers = False
  13.         .FillAdjacentFormulas = False
  14.         .PreserveFormatting = True
  15.         .RefreshOnFileOpen = False
  16.         .BackgroundQuery = True
  17.         .RefreshStyle = xlInsertDeleteCells
  18.         .SavePassword = False
  19.         .SaveData = True
  20.         .AdjustColumnWidth = True
  21.         .RefreshPeriod = 0
  22.         .WebSelectionType = xlAllTables
  23.         .WebFormatting = xlWebFormattingNone
  24.         .WebPreFormattedTextToColumns = True
  25.         .WebConsecutiveDelimitersAsOne = True
  26.         .WebSingleBlockTextImport = False
  27.         .WebDisableDateRecognition = False
  28.         .WebDisableRedirections = False
  29.         .Refresh BackgroundQuery:=False
  30.     End With
  31.         With ActiveSheet.QueryTables.Add(Connection:= _
  32.         "URL;http://cagweb02.hct.com.tw/pls/cagweb/CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=04413850008" _
  33.         , Destination:=Range("$A$7"))
  34.         .Name = _
  35.         "CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=04413850008"
  36.         .FieldNames = True
  37.         .RowNumbers = False
  38.         .FillAdjacentFormulas = False
  39.         .PreserveFormatting = True
  40.         .RefreshOnFileOpen = False
  41.         .BackgroundQuery = True
  42.         .RefreshStyle = xlInsertDeleteCells
  43.         .SavePassword = False
  44.         .SaveData = True
  45.         .AdjustColumnWidth = True
  46.         .RefreshPeriod = 0
  47.         .WebSelectionType = xlAllTables
  48.         .WebFormatting = xlWebFormattingNone
  49.         .WebPreFormattedTextToColumns = True
  50.         .WebConsecutiveDelimitersAsOne = True
  51.         .WebSingleBlockTextImport = False
  52.         .WebDisableDateRecognition = False
  53.         .WebDisableRedirections = False
  54.         .Refresh BackgroundQuery:=False
  55.     End With
  56.             With ActiveSheet.QueryTables.Add(Connection:= _
  57.         "URL;http://cagweb02.hct.com.tw/pls/cagweb/CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=03909690001" _
  58.         , Destination:=Range("$A$13"))
  59.         .Name = _
  60.         "CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=03909690001"
  61.         .FieldNames = True
  62.         .RowNumbers = False
  63.         .FillAdjacentFormulas = False
  64.         .PreserveFormatting = True
  65.         .RefreshOnFileOpen = False
  66.         .BackgroundQuery = True
  67.         .RefreshStyle = xlInsertDeleteCells
  68.         .SavePassword = False
  69.         .SaveData = True
  70.         .AdjustColumnWidth = True
  71.         .RefreshPeriod = 0
  72.         .WebSelectionType = xlAllTables
  73.         .WebFormatting = xlWebFormattingNone
  74.         .WebPreFormattedTextToColumns = True
  75.         .WebConsecutiveDelimitersAsOne = True
  76.         .WebSingleBlockTextImport = False
  77.         .WebDisableDateRecognition = False
  78.         .WebDisableRedirections = False
  79.         .Refresh BackgroundQuery:=False
  80.     End With
  81.              With ActiveSheet.QueryTables.Add(Connection:= _
  82.         "URL;http://cagweb02.hct.com.tw/pls/cagweb/CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=02074160008" _
  83.         , Destination:=Range("$A$19"))
  84.         .Name = _
  85.         "CAGWEB.C_PCTHA272?pArea=000&pSCd=6006&pDayFlg=0&pDayFlg1=1&pSearchFlg=2&pCustFlg=02074160008"
  86.         .FieldNames = True
  87.         .RowNumbers = False
  88.         .FillAdjacentFormulas = False
  89.         .PreserveFormatting = True
  90.         .RefreshOnFileOpen = False
  91.         .BackgroundQuery = True
  92.         .RefreshStyle = xlInsertDeleteCells
  93.         .SavePassword = False
  94.         .SaveData = True
  95.         .AdjustColumnWidth = True
  96.         .RefreshPeriod = 0
  97.         .WebSelectionType = xlAllTables
  98.         .WebFormatting = xlWebFormattingNone
  99.         .WebPreFormattedTextToColumns = True
  100.         .WebConsecutiveDelimitersAsOne = True
  101.         .WebSingleBlockTextImport = False
  102.         .WebDisableDateRecognition = False
  103.         .WebDisableRedirections = False
  104.         .Refresh BackgroundQuery:=False
  105.     End With
  106.     Dim URL As String, xDate As Date
  107.    
  108.     With ActiveSheet.QueryTables.Add(Connection:= _
  109.         "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" _
  110.         , Destination:=Range("$A$1"))
  111.         .Name = _
  112.         "CTGPS01R2.aspx?ScanDay=[color=Red]20131218[/color]&Stor=6006&StorDesc=%e5%8f%b0%e5%8d%97&CseNo=All&OpeCd=All"
  113.         .FieldNames = True
  114.         .RowNumbers = False
  115.         .FillAdjacentFormulas = False
  116.         .PreserveFormatting = True
  117.         .RefreshOnFileOpen = False
  118.         .BackgroundQuery = True
  119.         .RefreshStyle = xlInsertDeleteCells
  120.         .SavePassword = False
  121.         .SaveData = True
  122.         .AdjustColumnWidth = True
  123.         .RefreshPeriod = 0
  124.         .WebSelectionType = xlSpecifiedTables
  125.         .WebFormatting = xlWebFormattingNone
  126.         .WebTables = "1"
  127.         .WebPreFormattedTextToColumns = True
  128.         .WebConsecutiveDelimitersAsOne = True
  129.         .WebSingleBlockTextImport = False
  130.         .WebDisableDateRecognition = False
  131.         .WebDisableRedirections = False
  132.         .Refresh BackgroundQuery:=False
  133.     End With
  134. 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文一開始就說明 我公司室內網  一般民網連不上去
  1.     Dim xDate As Date
  2.     xDate = Date
  3.     Range("Sheet2!$E$6").Value = (xDate)

  4.     With ActiveSheet.QueryTables.Add(Connection:= _
  5.         "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" _
  6.         , Destination:=Range("Sheet1!$A$25"))
  7.         .Name = _
  8.         "CTGPS01R2.aspx?ScanDay=" & Format(xDate, "YYYYMMDD") & "&Stor=6006&StorDesc=%e5%8f%b0%e5%8d%97&CseNo=All&OpeCd=All"
  9.         .FieldNames = True
  10.         .RowNumbers = False
  11.         .FillAdjacentFormulas = False
  12.         .PreserveFormatting = True
  13.         .RefreshOnFileOpen = False
  14.         .BackgroundQuery = True
  15.         .RefreshStyle = xlInsertDeleteCells
  16.         .SavePassword = False
  17.         .SaveData = True
  18.         .AdjustColumnWidth = True
  19.         .RefreshPeriod = 0
  20.         .WebSelectionType = xlSpecifiedTables
  21.         .WebFormatting = xlWebFormattingNone
  22.         .WebTables = "1"
  23.         .WebPreFormattedTextToColumns = True
  24.         .WebConsecutiveDelimitersAsOne = True
  25.         .WebSingleBlockTextImport = False
  26.         .WebDisableDateRecognition = False
  27.         .WebDisableRedirections = False
  28.         .Refresh BackgroundQuery:=False
  29.     End With
複製代碼
以上這段我卡住比較久
" & Format(xDate, "YYYYMMDD") & " 用法



Dim xDate As Date
    xDate = Date

宣告當日時間   

剩下登入畫面我上網找人家PO出來的成品自己修改進去的




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)