返回列表 上一主題 發帖

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

回復 1# jovi0801
試試看
  1. Sub Ex() 'DatePart("WW", .Rows(xi).Cells(1))  傳回第幾週
  2.     Dim Rng As Range, xi As Integer
  3.     With Sheets("日價格").Range("A1").CurrentRegion.Rows
  4.         Set Rng = .Rows(1)
  5.         xi = 2
  6.         Do
  7.             If DatePart("WW", .Rows(xi).Cells(1)) <> DatePart("WW", .Rows(xi + 1).Cells(1)) Then Set Rng = Union(Rng, .Rows(xi))
  8.             xi = xi + 1
  9.            If xi = .Rows.Count Then Set Rng = Union(Rng, .Rows(xi))
  10.         Loop While xi < .Rows.Count
  11.     End With
  12.     With Sheets("周價格")
  13.         .UsedRange.Clear
  14.         Rng.Copy .[a1]
  15.     End With
  16. End Sub
複製代碼

TOP

本帖最後由 GBKEE 於 2012-7-10 16:02 編輯

回復 4# jovi0801
  1. Sub Ex() 'DatePart("WW", .Rows(xi).Cells(1))  傳回第幾週
  2.     Dim AR, xi As Integer, xAr As Integer, Rng As Range
  3.     With Sheets("日價格")
  4.         AR = Application.Transpose(.Range("A1").CurrentRegion.Rows(1).Value)         '取得欄位
  5.         xAr = 2
  6.         xi = 2
  7.         ReDim Preserve AR(1 To 5, 1 To xAr)                             '新增一維空白陣列
  8.         Set Rng = .Cells(xi, "A")                                       '一週營業的第一天日期位置
  9.         Do While .Cells(xi, "A") <> ""
  10.             If DatePart("WW", .Cells(xi, "A")) <> DatePart("WW", .Cells(xi + 1, "A")) Then
  11.                 Set Rng = Range(Rng, .Cells(xi, "E"))                   '一週營業的第一天日期位置  到  最後第一天收盤價位置
  12.                 AR(1, xAr) = Rng.Cells(1)                               '日期  Rng.Cells(1) 日期: 週一  或 .Cells(xi, "A") 日期: 週五(最後一天)
  13.                 AR(2, xAr) = Rng.Cells(1, 2)                            '開盤價
  14.                 AR(3, xAr) = Application.Max(Rng.Columns(3))            '最高價
  15.                 AR(4, xAr) = Application.Min(Rng.Columns(4))             '最低價
  16.                 AR(5, xAr) = .Cells(xi, "E")                            '收盤價
  17.                 If .Cells(xi + 1, "A") <> "" Then
  18.                     Set Rng = .Cells(xi + 1, "a")
  19.                     xAr = xAr + 1
  20.                     ReDim Preserve AR(1 To 5, 1 To xAr)
  21.                 End If
  22.             End If
  23.             xi = xi + 1
  24.         Loop
  25.     End With
  26.     With Sheets("周價格")
  27.         .UsedRange.Clear
  28.         .[A1].Resize(xAr, 5) = Application.Transpose(AR)
  29.     End With
  30. End Sub
複製代碼

TOP

回復 9# c_c_lai
加上期間這不錯啊 ,但可不需新增變數來顯示.   
AR(6, xAr) = scope & "-" & .Cells(xi, "A")   ->      AR(6, xAr) = Rng.Cells(1) & "-" & .Cells(xi, "A")

TOP

        靜思自在 : 人的心地是一畦田,土地沒有播下好種子,也長不出好的果實。 -
返回列表 上一主題