返回列表 上一主題 發帖

[發問] 資料排序反轉

回復 9# c_c_lai


    又來請教大大
從網路下載的資料是日期反的,而圖表反向,
請問 大大要修改那個地方
謝謝

test.rar (979.9 KB)

TOP

本帖最後由 c_c_lai 於 2013-10-11 20:02 編輯

回復 11# wufonna
  1. Sub MySort()
  2.      Dim EndKBarRow As Long
  3.         
  4.     With Sheets("data")
  5.         EndKBarRow = .Range("A" & Rows.Count).End(xlUp).Row
  6.    
  7.         With .Sort
  8.             .SortFields.clear
  9.             .SortFields.Add Key:=Range("A5"), SortOn:=xlSortOnValues, Order:=xlAscending
  10.             '  資料由小而大依序排序
  11.             '  .SortFields.Add Key:=Range("A5"), SortOn:=xlSortOnValues, Order:=xlDescending
  12.             '  資料由大而小依序排序
  13.             .SetRange Range("A5:R" & EndKBarRow)
  14.             ' .Header = xlNone
  15.             .Apply
  16.         End With
  17.     End With
  18. End Sub
複製代碼

TOP

回復 12# c_c_lai


    感謝大大,已作出大盤,謝謝 ^o^

台灣加權指數-報酬率區間圖II.rar (956.47 KB)

TOP

回復 13# wufonna
你在 drawCharts() 最前、及最後加上一行 (如下) 繪圖完成後畫面會更清爽。
  1. Sub drawCharts()
  2.     Dim toindexRows As Single, totalRows As Single, totalRows2 As Single
  3.     Dim VIMax As Single, VIMin As Single
  4.     Dim xRow, yCol, cHeight, cWidth As Integer
  5.     Dim text As String
  6.     Dim chartname As String
  7.     Dim sRowHeight As Single

  8.     '  xRow = 3
  9.     xRow = 1
  10.     yCol = 1
  11.     '  cHeight = 450      'CHeight 定義為圖表所佔的列高
  12.     '  cWidth = 700
  13.     cHeight = 360         ' CHeight 定義為圖表所佔的列高
  14.     cWidth = 720
複製代碼
  1.     Sheets("chart").[A1].Select
  2. End Sub
複製代碼

TOP

本帖最後由 c_c_lai 於 2013-10-12 07:43 編輯

回復 13# wufonna
我在 drawCharts() 加了一小段程式,它將標題移出繪圖區,
如此,比較不會壓到 K 線圖的繪製 (附上: 前、後的圖表提供比較):
  1.         .ChartArea.Height = cHeight                     ' 圖表高度
  2.         .ChartArea.Width = cWidth                       ' 圖表寬度
  3.          '
  4.         With .PlotArea                          ' 圖表的繪圖區
  5.             .Top = 20                           ' 圖表的實際繪圖區起始位置
  6.             .Left = 1
  7.             .Width = .Parent.ChartArea.Width
  8.             .Height = .Parent.ChartArea.Height
  9.             .Interior.ColorIndex = xlNone
  10.             .InsideHeight = cHeight - 70        ' 調整圖表的繪圖區內部高度 (為配合.Legend.Position = xlBottom )
  11.         End With
複製代碼

TOP

回復 15# c_c_lai


    謝謝 大大
   找了好久來到了這版版,又看到了大大的 K 線股票圖如何能與主力、散戶、及成交量線共存? 線圖共存,學習做流河圖,謝謝 大大 無私的教導。

台灣加權指數-報酬率區間圖III.rar (959.32 KB)

TOP

回復 16# wufonna
我將 "台灣加權指數-報酬率區間圖III"  稍稍整理了一下,
並加以程式碼對位,希望對你能有所助益。
drawCharts2() 是我常應用的繪圖方式,供你參考!
台灣加權指數-報酬率區間圖III.rar (921.68 KB)

TOP

回復 17# c_c_lai


    謝謝 大大  ^0^

TOP

河流圖網址無效
參考一些資料
https://finance.yahoo.com/quote/2330.TW/history?period1=1575244800&period2=1631404800&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true


Private Const Unix1970 As Long = 25569 'CDbl(DateSerial(1970, 1, 1))

'Dim s As String

Public Function Date2Unix(ByVal vDate As Date) As Long

Date2Unix = DateDiff("s", Unix1970, vDate)


End Function

Sub test()
Debug.Print s

Debug.Print DateDiff("s", Unix1970, vDate)
Debug.Print Date2Unix("2019/6/7")
Debug.Print Date2Unix(Date - 365 * 3)  '取三年
Debug.Print Now()
Debug.Print Date
Debug.Print Date2Unix(Date)
Debug.Print "https://finance.yahoo.com/quote/2330.TW/history?period1=" & Date2Unix("2019/6/7") & "&period2=" & Date2Unix(Date) & "&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true"

End Sub



'debug
'

'-2209161600
' 1559865600
' 1536796800
'2021/9/12 下午 09:45:15
'2021/9/12
' 1631404800
'https://finance.yahoo.com/quote/2330.TW/history?period1=1559865600&period2=1631404800&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true





'https://white5168.blogspot.com/2017/03/unix-timestamp-excel-vba.html?fbclid=IwAR1FQDyY3A1utM3vaT24SKcoybW02PUycFBWuW8JXncdCfzk5wTEDZgi3vw#.YT33950zbcs
'https://www.cadch.com/article/timestamp/index.php
'https://finance.yahoo.com/quote/2330.TW/history?period1=1575244800&period2=1631404800&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true
'http://forum.twbts.com/thread-22471-1-2.html?fbclid=IwAR0V3t8wojYYInLQXamUi6ZJqPeSRQGHhSR9RBia2xRh5YJw2ZIUvxMDWSw
'參考資料

'debug.print https://finance.yahoo.com/quote/2330.TW/history?period1=" & Date2Unix("2019/6

TOP

回復 1# wufonna

https://www.mobile01.com/topicdetail.php?f=511&t=4737630&p=28

參考 274樓 修改 河流圖
  1. Sub Getyahoofinance_Jsondata()

  2.     Dim Xmlhttp As Object, FileName As String, Url As String, urla As String, Crumbkey As String, stock As String, startday As String, endday As String
  3.     Dim Jsondata As Object, DecodeJson, temp
  4.     Dim TimeStamp, adjclose, quote_Open, quote_High, quote_Low, quote_Close, quote_Volume
  5.     Dim ttt, I
  6.     Set Jsondata = CreateObject("HtmlFile")
  7.     Jsondata.write "<script>document.JsonParse=function (s) {return eval('(' + s + ')');}</script>"

  8. '    Sheets("工作表1").Cells.Clear
  9. '    Sheets("工作表1").Range("a1:g1") = Array("Date", "Open", "HIgh", "Low", "Close", "Adj Close", "Volume")
  10.     With 工作表1
  11.           Dim AR
  12.            AR = .Range("A1:G4")
  13.            .Range("A:G") = ""
  14.            .Range("A1:G4") = AR
  15.     End With
  16.    
  17.     stock = InputBox("股票代號", , "^TWII")
  18.     startday = Format(InputBox("開始日期(8碼數字)", , "20170101"), "####/##/##")
  19.     endday = Format(InputBox("結束日期(8碼數字)", , Format(Date, "yyyymmdd")), "####/##/##")
  20.    
  21.     If startday > endday Or stock = "" Or startday = "" Or endday = "" Then
  22.         MsgBox "資料輸入錯誤", vbOKOnly, "請重新輸入"
  23.         Exit Sub
  24.     End If
  25.    
  26.   
  27.     ttt = Timer
  28.    
  29.     Url = "https://finance.yahoo.com/quote/" & stock & "/history?period1=" & DataToUnixTime(startday) & "&period2=" & DataToUnixTime(endday) & "&interval=1d&filter=history&frequency=1d"
  30.    
  31.     Set Xmlhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
  32.     With Xmlhttp
  33.    
  34.        .Open "GET", Url, False
  35.        .send
  36.       
  37.         Crumbkey = Left(Split(.responsetext, """CrumbStore"":{""crumb"":""")(1), 11)
  38.         urla = "https://query2.finance.yahoo.com/v8/finance/chart/" & stock & "?formatted=true&crumb=" & Crumbkey & "&lang=en-US&region=US&period1=" & DataToUnixTime(startday) & "&period2=" & DataToUnixTime(endday) & "&interval=1d&events=div%7Csplit&corsDomain=finance.yahoo.com"

  39.        .Open "GET", urla, False
  40.        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  41.        .setRequestHeader "Referer", Url
  42.        .send
  43.         
  44.         Set DecodeJson = Jsondata.JsonParse(.responsetext)
  45.         Set temp = CallByName(CallByName(CallByName(DecodeJson, "chart", VbGet), "result", VbGet), "0", VbGet)
  46.         
  47.         TimeStamp = Split(CallByName(temp, "timestamp", VbGet), ",")
  48.         adjclose = Split(CallByName(CallByName(CallByName(CallByName(temp, "indicators", VbGet), "adjclose", VbGet), "0", VbGet), "adjclose", VbGet), ",")
  49.         quote_Open = Split(CallByName(CallByName(CallByName(CallByName(temp, "indicators", VbGet), "quote", VbGet), "0", VbGet), "open", VbGet), ",")
  50.         quote_High = Split(CallByName(CallByName(CallByName(CallByName(temp, "indicators", VbGet), "quote", VbGet), "0", VbGet), "high", VbGet), ",")
  51.         quote_Low = Split(CallByName(CallByName(CallByName(CallByName(temp, "indicators", VbGet), "quote", VbGet), "0", VbGet), "low", VbGet), ",")
  52.         quote_Close = Split(CallByName(CallByName(CallByName(CallByName(temp, "indicators", VbGet), "quote", VbGet), "0", VbGet), "close", VbGet), ",")
  53.         quote_Volume = Split(CallByName(CallByName(CallByName(CallByName(temp, "indicators", VbGet), "quote", VbGet), "0", VbGet), "volume", VbGet), ",")
  54.         
  55.       
  56.     End With
  57.    
  58.    
  59.     With 工作表1
  60.         Application.ScreenUpdating = False
  61.         For I = 0 To UBound(TimeStamp)
  62.             .Cells(I + 5, 1) = Format(TimeStamp(I) / 86400 + #1/1/1970 8:00:00 AM#, "yyyy/mm/dd")
  63.             .Cells(I + 5, 2) = quote_Open(I)
  64.             .Cells(I + 5, 3) = quote_High(I)
  65.             .Cells(I + 5, 4) = quote_Low(I)
  66.             .Cells(I + 5, 5) = quote_Close(I)
  67.             .Cells(I + 5, 6) = adjclose(I)
  68.             .Cells(I + 5, 7) = quote_Volume(I)
  69.             .Cells.EntireColumn.AutoFit
  70.        '     .Cells(1, 1).Select
  71.         Next I
  72. '        Application.ScreenUpdating = True
  73. '        MsgBox "開始日期" & startday & vbNewLine & "結束日期" & endday & vbNewLine & _
  74. '        "股票代號" & stock & vbNewLine & "資料筆數" & .Range("a1").CurrentRegion.Rows.Count - 1 & "筆" & vbNewLine & _
  75. '        "使用時間" & Round(Timer - ttt, 2) & "秒", vbOKOnly, "下載完成"
  76.     End With
  77.    
  78.    
  79.    
  80.     Set Xmlhttp = Nothing
  81.     Set DecodeJson = Nothing
  82.     Set temp = Nothing
  83.    
  84. End Sub

  85. Sub DeleteEmptyRows()
  86. Dim LastRow As Long, r As Long

  87.    
  88. With 工作表1
  89. LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
  90. LastRow = LastRow + .Range("A" & .Rows.Count).End(xlUp).Row

  91. For r = LastRow To 5 Step -1
  92.     If .Cells(r, 2) = "" Then
  93.     Debug.Print .Cells(r, 1).Value
  94.    
  95.     .Rows(r).Delete
  96.     End If
  97.     Next r

  98. End With

  99.         Application.ScreenUpdating = True

  100. End Sub

  101. Function DataToUnixTime(dstring) As Long
  102.     DataToUnixTime = (DateValue(dstring) - #1/1/1970 8:00:00 AM#) * 86400
  103. End Function
複製代碼

台灣加權指數-報酬率區間圖III-20022xlsm.rar (596.46 KB)

TOP

        靜思自在 : 好事要提得起,是非要放得下,成就別人即是成就自己。
返回列表 上一主題