返回列表 上一主題 發帖

[發問] (已解決)計算最近一次漲跌價幅度

本帖最後由 GBKEE 於 2011-7-29 20:25 編輯

回復 3# freeffly
  1. Sub Ex()
  2. Dim C() As Integer, i As Integer, ii As Integer
  3. For i = 2 To Cells(Rows.Count, "M").End(xlUp).Row
  4. '迴圈 從第2列開始  到(To)  M欄最底端(Rows.Count) 往上到有資料(End(xlUp))的列號
  5.     If Application.CountA(Cells(i, "B").Resize(, 12)) >= 2 Then 'B欄Resize(擴充)12欄: 到M欄
  6.         ReDim C(1) As Integer                    '重新宣告陣列
  7.         For ii = 13 To 2 Step -1                  '迴圈 間隔(Step) -1 :由大往小 (下)
  8.             If Cells(i, ii) <> "" Then
  9.                 If (C(0)) = 0 Then
  10.                     C(0) = Cells(i, ii)
  11.                 ElseIf C(1) = 0 Then
  12.                     C(1) = Cells(i, ii)
  13.                 End If
  14.                 If C(0) <> 0 And C(1) <> 0 Then   '找到最接近的兩數
  15.                     Cells(i, "N") = (C(0) - C(1)) / C(1)
  16.                     Exit For
  17.                 End If
  18.             End If
  19.         Next
  20.     End If
  21. Next
  22. End Sub
複製代碼

TOP

回復 9# freeffly
If Application.CountA(Cells(i, "B").Resize(, 12)) >= 2 Then 'B欄Resize(擴充)12欄: 到M欄
上式 是檢查 到有2個數字以上
        ReDim C(1) As Integer        '重新宣告陣列   ->歸零    C(0), C(1) 設定最右邊最接近的2個數字

TOP

        靜思自在 : 成功是優點的發揮,失敗是缺點的累積。
返回列表 上一主題