返回列表 上一主題 發帖

在多個工作表作平均值計算,出現RANGE方法(_worksheet物件)失敗之錯誤

回復 1# tsunamix03

你這程式碼是否在  Sheets(2)工作表物件模組裡!

試試看
  1. Option Explicit
  2. Sub Ex()
  3. Dim col As Integer, p_today As Single, R As Integer, i As Integer, avg As Double
  4.     For i = 2 To 9                           '在工作表2至工作表9作計算
  5.         col = 3
  6.         With Sheets(i)
  7.             Do While .Cells(1, col) <> ""
  8.                 If IsNumeric(.Cells(65536, col + 1).End(xlUp).Value) Then     '如果是數字
  9.                     p_today = .Cells(65536, col + 1).End(xlUp).Value                '行號(col+1)  存有每天的資料
  10.                 Else
  11.                     p_today = 0
  12.                 End If
  13.                 ' Sheets(i).Activate
  14.                 ' Sheets(i).Cells(65536, col + 1).End(xlUp).Select
  15.                 R = .Cells(65536, col + 1).End(xlUp).Row
  16.                 avg = Application.Average(.Range(.Cells(R - 259, col + 1), .Cells(R, col + 1)))
  17.                 .Cells(65536, col).Value = avg            '行號col  放置260天平均值
  18.                 col = col + 2
  19.             Loop
  20.         End With
  21.     Next
  22. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 【行善要及時】行善要及時,功德要持續。如燒開水一般,未燒開之前千萬不要停熄火候,否則重來就太費事了。
返回列表 上一主題