返回列表 上一主題 發帖

[發問] 請問如何抓取氣象局opendata xml 問題

本帖最後由 GBKEE 於 2016-3-4 14:19 編輯

回復 9# yehmengfeng
試試看
UserForm 模組的程式碼
UserForm 需制定控制項 CommandButton1,ComboBox1,ComboBox2
  1. Option Explicit
  2. Dim Sh(1 To 2) As Worksheet
  3. Private Sub UserForm_Initialize()
  4.     Set Sh(1) = Sheets.Add  '設定新增工作表
  5.     Sh(1).Visible = False   '隱藏工作表
  6.     觀測查詢_設定
  7. End Sub
  8. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  9.     Application.DisplayAlerts = False
  10.     Sh(1).Delete
  11.     Application.DisplayAlerts = True
  12. End Sub
  13. Private Sub 觀測查詢_設定()
  14.     Dim Url As String, i As Double, op As Object, xDate As Date
  15.     Dim oHtmldoc As Object
  16.     Set oHtmldoc = CreateObject("htmlfile")
  17.     Url = "http://e-service.cwb.gov.tw/HistoryDataQuery/QueryDataController.do?command=viewMain"   '
  18.     With CreateObject("msxml2.xmlhttp")
  19.         .Open "Get", Url, False
  20.         .Send
  21.         oHtmldoc.write .responseText
  22.     End With
  23.     With ComboBox1
  24.         For Each op In oHtmldoc.all.tags("SELECT")(0)
  25.             .AddItem
  26.             .List(.ListCount - 1, 0) = op.innertext  '測站:名稱
  27.             .List(.ListCount - 1, 1) = op.Value      '測站:數值
  28.         Next
  29.         .ListIndex = 0
  30.     End With
  31.     With ComboBox2
  32.         For i = 0 To -59 Step -1  '60個月份
  33.             xDate = DateAdd("M", i, Date)
  34.             .AddItem
  35.             .List(.ListCount - 1, 0) = Format(xDate, "EE年MM月")
  36.             .List(.ListCount - 1, 1) = DateSerial(Year(xDate), Month(xDate), 1)
  37.         Next
  38.         .ListIndex = 0
  39.     End With
  40. End Sub
  41. Private Sub CommandButton1_Click() '按紐(資料查詢)
  42.      Dim surl As String, QT As Range, Qdate As Date, Station As String, Msg As Boolean
  43.     'http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?
  44.     surl = "URL;http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?"
  45.     surl = surl & "command=viewMain&station=" & ComboBox1.List(ComboBox1.ListIndex, 1)
  46.     Station = ComboBox1.List(ComboBox1.ListIndex, 0) & "(" & ComboBox1.List(ComboBox1.ListIndex, 1) & ")" '工作表名稱
  47.     On Error GoTo Make_station:  '處裡錯誤: 測站不存在
  48.     Set Sh(2) = Sheets(Station)  '指定工作表(測站)
  49.     On Error GoTo 0              '有錯誤不處理了
  50.     Application.ScreenUpdating = False
  51.     For Qdate = ComboBox2.List(ComboBox2.ListIndex, 1) To DateAdd("m", 1, ComboBox2.List(ComboBox2.ListIndex, 1)) - 1
  52.         surl = "URL;http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=" & ComboBox1.List(ComboBox1.ListIndex, 1) & "&datepicker=" & Format(Qdate, "yyyy-m-dd")
  53.         If Qdate > Date Then Exit For    '日期大於當日
  54.         If Not Sh(2).Range("A:A").Find(Qdate, LookIn:=xlFormulas) Is Nothing Then GoTo Ne  '日期資料已存在,下一日期迴圈
  55.         Application.StatusBar = "匯入 " & Qdate & " 資料..."  '狀態列的文字
  56.         With Sh(1)
  57.             .UsedRange.Delete
  58.             With .QueryTables.Add(Connection:=surl, Destination:=.Range("$A$1"))
  59.                 .WebTables = "MyTable"
  60.                 .WebFormatting = xlWebFormattingNone
  61.                 .Refresh BackgroundQuery:=False
  62.                 If .ResultRange.Rows.Count > 5 Then  '有資料
  63.                     Set QT = .ResultRange  '含表頭的資料範圍
  64.                     With Sh(2)
  65.                         If .UsedRange.Count = 1 Then                      '工作表(測站)為空白
  66.                             .Range("A1") = "觀測時間"
  67.                             .Range("A1").Resize(5).Merge
  68.                         Else
  69.                             Set QT = QT.Rows("6:" & QT.Rows.Count)         '不含表頭的資料
  70.                         End If
  71.                         With .Cells(Rows.Count, "b").End(xlUp)
  72.                             If .Row = 1 Then                '工作表(測站)為空白
  73.                                 QT.Copy .Cells
  74.                             Else                            '.Row = 4 ->沒有資料但有表頭
  75.                                 QT.Copy .Cells(IIf(.Row = 4, 3, 2))
  76.                             End If
  77.                         End With
  78.                         With .Range(.Cells(Rows.Count, "A").End(xlUp).Offset(1).Address & ":A" & Sh(2).Cells(Rows.Count, "B").End(xlUp).Row)
  79.                             .Cells = Qdate                      'A欄寫上日期
  80.                             .NumberFormatLocal = "yyyy-mm-dd"   '給予格式
  81.                         End With
  82.                     End With
  83.                 End If
  84.                 .Delete
  85.             End With
  86.         End With
  87. Ne:
  88.     Next
  89.     Application.StatusBar = False
  90.     Application.ScreenUpdating = True
  91.      '排序
  92.     With Sh(2).UsedRange.Offset(5)
  93.         .Sort Key1:=.Range("A1"), Order1:=xlAscending, Key2:=.Range("B1"), Order2:=xlAscending, Header:=xlNo
  94.     End With
  95.     Exit Sub
  96. Make_station:
  97.     Sheets.Add(Sheet1).Name = Station  '新增工作表(測站)
  98.     Resume   '回到錯誤的程式碼繼續程式
  99. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

謝謝stillfish00的協助,因為權限不足,所以無法查看,請見諒。
最近因為升級組長,所以工作量變大很多,還得多多的調適。
但有空還是在試做,希望能做的出來。
目前是把上面板主寫的 試著自已修改看看....,但還是串不起來。

TOP

GBKEE板主您好:
請問 我使用您po的程式試做,發現會一直出現 編輯錯誤:變數未定義
請問這要如何解決,一直找不出來那有問題點。

我查到的資料解釋:「在模組開頭寫了 Option Explicit,程式裡的變數就一定要先宣告才能使用,否則程式要執行的時候,VBA 解譯器會指出 "變數未定義"。
沒加 Option Explicit,VBA解譯器就不管程式裡的變數有沒有宣告, 好處是自由,壞處是若不小心打錯字就會被當成另一個變數, 例如: 有一變數名稱為 Customer,程式裡有一處誤打成 Custmoer,VBA 解譯器不會指出錯誤,它會認為 Customer和Custmoer是兩個不同的變數,因此這個程式執行的結果就不會正確。」

abbr_867650710b08c22e5ea3e2c8ad13e3be.png (76.61 KB)

abbr_867650710b08c22e5ea3e2c8ad13e3be.png

TOP

回復 13# yehmengfeng
我使用2003 正常
你試試修正看看
  1. Private Sub CommandButton1_Click() '按紐(資料查詢)
  2.      Dim surl As String, QT As Range, Qdate As Date, Station As String, Msg As Boolean
  3.     surl = "URL;http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?"
  4.     Stop
  5.     MsgBox ComboBox1
  6.     MsgBox ComboBox1.List(Me.ComboBox1.ListIndex, 1)  '加上ME是否可修正錯誤
  7.     MsgBox ComboBox1.List(ComboBox1.ListIndex, 1)
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

謝謝GBKEE板主  我試試看,謝謝您。

TOP

        靜思自在 : 一個缺口的杯子,如果換一個角度看它,它仍然是圓的。
返回列表 上一主題