所以我的代码工作正常,直到我尝试添加嵌套for循环,然后我开始得到运行时错误13.错误所在的行被***包围
这是我之前的代码:
Dim LR As Long
Dim ColLtr As String
Dim Ave As Double
Dim STDev As Double
For i = 1 To DateTime_Column
ColLtr = Replace(Cells(1, i).Address(True, False), "$1", "")
Ave = Application.Average(Range(ColLtr & "1:" & ColLtr & LR)) ' Based on all values
STDev = Application.STDev(Range(ColLtr & "1:" & ColLtr & LR)) ' Based on all values
Next i
以下是获取错误的代码:
Dim LR As Long
Dim ColLtr As String
Dim Ave As Double
Dim STDev As Double
Dim q As Integer
ReDim Range(LR1) As Variant
For i = 1 To DateTime_Column
Sheets(1).Select
ColLtr = Replace(Cells(1, i).Address(True, False), "$1", "")
***Ave = Application.Average(Range(ColLtr & "2:" & ColLtr & LR))*** ' Based on all values
For q = 1 To LR1
Range(q) = WorksheetFunction.Abs(Cells(q + 1, ColLtr) - Cells(q, ColLtr))
Next q
接下来我
我最初的想法是LR很长,所以我尝试将其更改为字符串数据类型,但这也没有用,也许它与for循环有关?
答案 0 :(得分:2)
将Range object之类的名称重新用于其他目的之外并不是一个好主意。在您的情况下尤其如此,因为您决定在重新定义范围时允许ActiveSheet property成为默认Range.Parent property。
Dim LR As Long, LR1 As Long, q As Long
Dim Ave As Double, STDev As Double
ReDim vals(LR1) As Variant
With Sheets(1)
For i = 1 To DateTime_Column
LR = .Cells(.Rows.Count, i).End(xlUp).Row
Ave = Application.Average(.Range(.Cells(2, i), .Cells(LR, i))) ' Based on all values
For q = 1 To LR1
vals(q) = WorksheetFunction.Abs(.Cells(q + 1, i) - .Cells(q, i))
Next q
Next i
End With
我删除了整个ColLtr
商家,并为所有Range.Parent property明确提供了Range和
.Cells个对象。
您仍然需要对vals
数组执行某些操作,并确保LR1
已正确定义。
答案 1 :(得分:1)
在VBA中运行公式或从单元格执行赋值时,<div class='modal' role='dialog' id="myModal">
....
</div>
的最常见原因是在单元格中具有错误值。此外,当您使用Run Time error 13
时,如果给定范围内的所有单元格都为空,您将得到相同的错误。
像这样测试:
Average