在多個工作表作平均值計算,出現RANGE方法(_worksheet物件)失敗之錯誤
- 帖子
- 16
- 主題
- 10
- 精華
- 0
- 積分
- 68
- 點名
- 0
- 作業系統
- win7
- 軟體版本
- 2003
- 閱讀權限
- 20
- 註冊時間
- 2015-2-26
- 最後登錄
- 2021-10-4
|
在多個工作表作平均值計算,出現RANGE方法(_worksheet物件)失敗之錯誤
下列程式碼在計算工作表2時運行正常,
但是一跳到工作表3計算平均值時卻會錯誤,
找了很久看不出問題所在,
懇請高手可以解惑,謝謝
Dim col As Integer, p_today As Single, row As Integer
For i = 2 To 9 '在工作表2至工作表9作計算
col = 3
Do While Sheets(i).Cells(1, col) <> ""
If IsNumeric(Sheets(i).Cells(65536, col + 1).End(xlUp).Value) Then '如果是數字
p_today = Sheets(i).Cells(65536, col + 1).End(xlUp).Value '行號(col+1) 存有每天的資料
Else
p_today = 0
End If
Sheets(i).Activate
Sheets(i).Cells(65536, col + 1).End(xlUp).Select
row = Selection.row()
avg = Application.Average(Range(Sheets(i).Cells(row - 259, col + 1), Sheets(i).Cells(row, col + 1)))
Sheets(i).Cells(65536, col).Value = avg '行號col 放置260天平均值
col = col + 2
Loop
Next |
|
|
|
|
|
|
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
2#
發表於 2015-3-20 06:43
| 只看該作者
回復 1# tsunamix03
你這程式碼是否在 Sheets(2)工作表物件模組裡!
試試看- Option Explicit
- Sub Ex()
- Dim col As Integer, p_today As Single, R As Integer, i As Integer, avg As Double
- For i = 2 To 9 '在工作表2至工作表9作計算
- col = 3
- With Sheets(i)
- Do While .Cells(1, col) <> ""
- If IsNumeric(.Cells(65536, col + 1).End(xlUp).Value) Then '如果是數字
- p_today = .Cells(65536, col + 1).End(xlUp).Value '行號(col+1) 存有每天的資料
- Else
- p_today = 0
- End If
- ' Sheets(i).Activate
- ' Sheets(i).Cells(65536, col + 1).End(xlUp).Select
- R = .Cells(65536, col + 1).End(xlUp).Row
- avg = Application.Average(.Range(.Cells(R - 259, col + 1), .Cells(R, col + 1)))
- .Cells(65536, col).Value = avg '行號col 放置260天平均值
- col = col + 2
- Loop
- End With
- Next
- End Sub
複製代碼 |
|
|
|
|
|
|
- 帖子
- 16
- 主題
- 10
- 精華
- 0
- 積分
- 68
- 點名
- 0
- 作業系統
- win7
- 軟體版本
- 2003
- 閱讀權限
- 20
- 註冊時間
- 2015-2-26
- 最後登錄
- 2021-10-4
|
3#
發表於 2015-3-20 23:43
| 只看該作者
|
|
|
|
|
|