ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] ½Ð°Ý¦p¦ó§ì¨ú®ð¶H§½opendata xml °ÝÃD

¥»©«³Ì«á¥Ñ GBKEE ©ó 2016-1-9 15:36 ½s¿è

¦^´_ 2# stillfish00
¦^´_ 3# joey0415
ºî¦X2¦ì,¨Ï¥Îªí³æ§e²{,¥i¿ï¾Ü¦a°Ï,¸ê®Æ®æ¦¡,®É¶¡. ¶×¤J©Ò­nªº®ð¶H¸ê®Æ.

·s¼W UserForm(ªí³æ) ¤¤¥[¤JComboBox1, ComboBox2, ComboBox3 ,CommandButton1
  1. Option Explicit
  2. Dim xObject()
  3. Private Sub UserForm_Initialize()
  4.     xObject = Array(ComboBox1, ComboBox2, ComboBox3)
  5.     Æ[´ú¬d¸ß_³]©w
  6.      Com
  7.      ComboBox1.Value = ComboBox1.List(0)
  8.      ComboBox2.Value = ComboBox2.List(0)
  9. End Sub
  10. Private Sub ComboBox1_Change() '´ú¡@¡@¯¸
  11.      Com
  12. End Sub
  13. Private Sub ComboBox2_Change() '¸ê®Æ®æ¦¡
  14.     Dim i  As Double
  15.     With ComboBox3
  16.         .Clear
  17.         Select Case ComboBox2.ListIndex
  18.             Case 0 '
  19.                 For i = Date - 1 To DateAdd("Q", -1, Date) Step -1
  20.                     .AddItem
  21.                     .List(.ListCount - 1) = Format(i, "YYYY-MM-DD")
  22.                 Next
  23.             Case 1
  24.                 i = Date
  25.                 Do
  26.                     .AddItem
  27.                     .List(.ListCount - 1) = Format(i, "YYYY-MM")
  28.                     i = DateAdd("M", -1, i)
  29.                 Loop Until Year(i) < Year(Date) - 1
  30.             Case 2
  31.                 For i = Year(Date) To Year(Date) - 1 Step -1
  32.                     .AddItem
  33.                     .List(.ListCount - 1) = i
  34.                 Next
  35.         End Select
  36.         If ComboBox2.ListIndex > -1 Then .Value = .List(0)
  37.     End With
  38.     Com
  39. End Sub
  40. Private Sub ComboBox3_Change()  '®É¡@¡@¶¡
  41.     Com
  42. End Sub
  43. Private Sub Com() '
  44.     Dim E As Variant
  45.     With CommandButton1
  46.         .Enabled = True
  47.         For Each E In xObject
  48.             If E.ListIndex = -1 Then .Enabled = False '«ö¯Ã(Æ[´ú¸ê®Æ¬d¸ß):¤£¥i¥Î
  49.         Next
  50.     End With
  51. End Sub
  52. Private Sub CommandButton1_Click() '«ö¯Ã(Æ[´ú¸ê®Æ¬d¸ß)
  53.      Dim surl As String, QT As QueryTable
  54.     'http://e-service.cwb.gov.tw/HistoryDataQuery/YearDataController.do?command=viewMain&station=467410&datepicker=2016
  55.     'http://e-service.cwb.gov.tw/HistoryDataQuery/MonthDataController.do?"
  56.     'http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?
  57.     surl = "URL; http://e-service.cwb.gov.tw/HistoryDataQuery/"
  58.     Select Case ComboBox2.ListIndex
  59.         Case 0
  60.             surl = surl & "DayDataController.do?"
  61.         Case 1
  62.             surl = surl & "MonthDataController.do?"
  63.         Case 2
  64.             surl = surl & "YearDataController.do?"
  65.     End Select
  66.     surl = surl & "command=viewMain&station=" & ComboBox1.List(ComboBox1.ListIndex, 1)
  67.     surl = surl & "&datepicker=" & ComboBox3
  68.     With ActiveSheet
  69.         .Cells.Delete
  70.         Set QT = .QueryTables.Add(Connection:=surl, Destination:=.Range("$A$1"))
  71.         With QT
  72.             .WebFormatting = xlWebFormattingNone
  73.             .Refresh BackgroundQuery:=False
  74.             .Delete
  75.         End With
  76.     End With
  77.     Set QT = Nothing
  78. End Sub
  79. Private Sub Æ[´ú¬d¸ß_³]©w()
  80.     Dim i As Double, E As Object, op As Object
  81.     Dim oXmlhttp As Object, oHtmldoc As Object, Url As String
  82.     Set oXmlhttp = CreateObject("msxml2.xmlhttp")
  83.     Set oHtmldoc = CreateObject("htmlfile")
  84.     Url = "http://e-service.cwb.gov.tw/HistoryDataQuery/QueryDataController.do?command=viewMain"   '
  85.     With oXmlhttp
  86.         .Open "Get", Url, False
  87.         .Send
  88.         oHtmldoc.write .responseText
  89.     End With
  90.     With oHtmldoc
  91.         Set E = .all.tags("SELECT")(0)
  92.         Set op = .all.tags("option")
  93.     End With
  94.     For i = 0 To op.Length - 1
  95.         If i <= E.Length - 1 Then
  96.             With ComboBox1
  97.                 .AddItem
  98.                 .List(.ListCount - 1, 0) = op(i).innertext  '´ú¯¸:¦WºÙ
  99.                 .List(.ListCount - 1, 1) = op(i).Value      '´ú¯¸:¼Æ­È
  100.             End With
  101.         Else
  102.             With ComboBox2
  103.                 .AddItem
  104.                 .List(.ListCount - 1, 0) = op(i).innertext  '¸ê®Æ®æ¦¡:¦WºÙ
  105.             End With
  106.         End If
  107.     Next
  108. End Sub
½Æ»s¥N½X
·P®¦ªº¤ß......(¦b³Â»¶®a±Ú°Q½×°Ï.¥Î¤ß¾Ç²ß·|¦³¶i¨Bªº)
¦ý¸ê·½µL­­,«á´©¦³­­,  ¤@¤Ñ1¤¸ªºÃÙ§U,¤H¤H¦³¯à¤O.

TOP

¦^´_ 5# c_c_lai

ÁÂÁ§Aªº´ú¸Õ¡A2003¥iªÅ¤@®æ¡C
·P®¦ªº¤ß......(¦b³Â»¶®a±Ú°Q½×°Ï.¥Î¤ß¾Ç²ß·|¦³¶i¨Bªº)
¦ý¸ê·½µL­­,«á´©¦³­­,  ¤@¤Ñ1¤¸ªºÃÙ§U,¤H¤H¦³¯à¤O.

TOP

¥»©«³Ì«á¥Ñ GBKEE ©ó 2016-3-4 14:19 ½s¿è

¦^´_ 9# yehmengfeng
¸Õ¸Õ¬Ý
UserForm ¼Ò²Õªºµ{¦¡½X
UserForm »Ý¨î©w±±¨î¶µ 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  '³]©w·s¼W¤u§@ªí
  5.     Sh(1).Visible = False   'ÁôÂäu§@ªí
  6.     Æ[´ú¬d¸ß_³]©w
  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 Æ[´ú¬d¸ß_³]©w()
  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  '´ú¯¸:¦WºÙ
  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() '«ö¯Ã(¸ê®Æ¬d¸ß)
  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) & ")" '¤u§@ªí¦WºÙ
  47.     On Error GoTo Make_station:  '³B¸Ì¿ù»~: ´ú¯¸¤£¦s¦b
  48.     Set Sh(2) = Sheets(Station)  '«ü©w¤u§@ªí(´ú¯¸)
  49.     On Error GoTo 0              '¦³¿ù»~¤£³B²z¤F
  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    '¤é´Á¤j©ó·í¤é
  54.         If Not Sh(2).Range("A:A").Find(Qdate, LookIn:=xlFormulas) Is Nothing Then GoTo Ne  '¤é´Á¸ê®Æ¤w¦s¦b,¤U¤@¤é´Á°j°é
  55.         Application.StatusBar = "¶×¤J " & Qdate & " ¸ê®Æ..."  'ª¬ºA¦Cªº¤å¦r
  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  '§tªíÀYªº¸ê®Æ½d³ò
  64.                     With Sh(2)
  65.                         If .UsedRange.Count = 1 Then                      '¤u§@ªí(´ú¯¸)¬°ªÅ¥Õ
  66.                             .Range("A1") = "Æ[´ú®É¶¡"
  67.                             .Range("A1").Resize(5).Merge
  68.                         Else
  69.                             Set QT = QT.Rows("6:" & QT.Rows.Count)         '¤£§tªíÀYªº¸ê®Æ
  70.                         End If
  71.                         With .Cells(Rows.Count, "b").End(xlUp)
  72.                             If .Row = 1 Then                '¤u§@ªí(´ú¯¸)¬°ªÅ¥Õ
  73.                                 QT.Copy .Cells
  74.                             Else                            '.Row = 4 ->¨S¦³¸ê®Æ¦ý¦³ªíÀY
  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Äæ¼g¤W¤é´Á
  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  '·s¼W¤u§@ªí(´ú¯¸)
  98.     Resume   '¦^¨ì¿ù»~ªºµ{¦¡½XÄ~Äòµ{¦¡
  99. End Sub
½Æ»s¥N½X
·P®¦ªº¤ß......(¦b³Â»¶®a±Ú°Q½×°Ï.¥Î¤ß¾Ç²ß·|¦³¶i¨Bªº)
¦ý¸ê·½µL­­,«á´©¦³­­,  ¤@¤Ñ1¤¸ªºÃÙ§U,¤H¤H¦³¯à¤O.

TOP

¦^´_ 13# yehmengfeng
§Ú¨Ï¥Î2003 ¥¿±`
§A¸Õ¸Õ­×¥¿¬Ý¬Ý
  1. Private Sub CommandButton1_Click() '«ö¯Ã(¸ê®Æ¬d¸ß)
  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)  '¥[¤WME¬O§_¥i­×¥¿¿ù»~
  7.     MsgBox ComboBox1.List(ComboBox1.ListIndex, 1)
½Æ»s¥N½X
·P®¦ªº¤ß......(¦b³Â»¶®a±Ú°Q½×°Ï.¥Î¤ß¾Ç²ß·|¦³¶i¨Bªº)
¦ý¸ê·½µL­­,«á´©¦³­­,  ¤@¤Ñ1¤¸ªºÃÙ§U,¤H¤H¦³¯à¤O.

TOP

        ÀR«ä¦Û¦b : µêªÅ¦³ºÉ¡D§ÚÄ@µL½a¡AµoÄ@®e©ö¦æÄ@Ãø¡C
ªð¦^¦Cªí ¤W¤@¥DÃD