返回列表 上一主題 發帖

[發問] 請問日價格怎麼轉變成週價格呢?

回復 5# GBKEE
回復 8# jovi0801
我加上了一欄 "期間" (scope) 如此可以清楚地看出它們的歸屬。
純參考,希望 GBKEE 大大莫介意。
  1. Sub Ex()                           ' DatePart("WW", .Rows(xi).Cells(1))  傳回第幾週
  2.     Dim AR, xi As Integer, xAr As Integer, Rng As Range, scope As String
  3.    
  4.     With Sheets("日價格")
  5.         AR = Application.Transpose(.Range("A1").CurrentRegion.Rows(1).Value)     ' 取得欄位
  6.         xAr = 2
  7.         xi = 2
  8.         ReDim Preserve AR(1 To 6, 1 To xAr)                      ' 新增一維空白陣列
  9.         
  10.         Set Rng = .Cells(xi, "A")                                ' 一週營業的第一天日期位置
  11.         scope = .Cells(xi, "A")
  12.         
  13.         Do While .Cells(xi, "A") <> ""
  14.             If DatePart("WW", .Cells(xi, "A")) <> DatePart("WW", .Cells(xi + 1, "A")) Then
  15.                 Set Rng = Range(Rng, .Cells(xi, "E"))            ' 一週營業的第一天日期位置  到  最後第一天收盤價位置
  16.                 AR(1, xAr) = Rng.Cells(1)           ' 日期  Rng.Cells(1) 日期: 週一  或 .Cells(xi, "A") 日期: 週五(最後一天)
  17.                 AR(2, xAr) = Rng.Cells(1, 2)                     ' 開盤價
  18.                 AR(3, xAr) = Application.Max(Rng.Columns(3))     ' 最高價
  19.                 AR(4, xAr) = Application.Min(Rng.Columns(4))     ' 最低價
  20.                 AR(5, xAr) = .Cells(xi, "E")                     ' 收盤價
  21.                 AR(6, xAr) = scope & "-" & .Cells(xi, "A")
  22.                
  23.                 If .Cells(xi + 1, "A") <> "" Then
  24.                     Set Rng = .Cells(xi + 1, "A")
  25.                     xAr = xAr + 1
  26.                     scope = .Cells(xi + 1, "A")
  27.                     ReDim Preserve AR(1 To 6, 1 To xAr)
  28.                 End If
  29.             End If
  30.             xi = xi + 1
  31.         Loop
  32.     End With
  33.    
  34.     With Sheets("周價格")
  35.         .UsedRange.Clear
  36.         .[A1].Resize(xAr, 6) = Application.Transpose(AR)
  37.     End With
  38. End Sub
複製代碼
日價格怎麼轉變成週價格.rar (9.85 KB)

TOP

回復 10# GBKEE
謝謝指導!
目前我對於 Set Rng 的應用尚未十分孰悉,可能是以往沒接觸過Excel
的緣故。 我會再加強這方面的應用, 謝謝您!

TOP

        靜思自在 : 小事不做、大事難成。
返回列表 上一主題