我的代码如下。当我运行它时,我得到了一个
1004应用程序定义或对象定义错误
在这一行:
Set MLNRange = Sheets("GJR GARCH").Range(Cells(2, j).Value, Cells(1063, j + 4).Value)
我是VBA的新手,我不明白出了什么问题。
Sub MLNRealworldtransfer()
Dim MLNRange As Range
Dim MLNDensity As Double
For i = 10 To 23
For j = 1 To 116
Set MLNRange = Sheets("GJR GARCH").Range(Cells(2, j).Value, Cells(1063, j + 4).Value)
MLNDensity = Application.WorksheetFunction.VLookup(Sheets("GJR GARCH").Cells(i, 122).Value, MLNRange, 5, "True")
MLNDensity = Sheets("GJR GARCH").Cells(i, 125)
i = i + 1
Next
j = j + 9
Next
修改1:
我做了更改,我接受了编译错误子或以下代码中未定义的函数
Sub MLNRealworldtran()
Dim MLNRange As Range
Dim MLNDensity As Double
For i = 10 To 23 Step 2
For j = 1 To 116 Step 10
Set MLNRange = Sheets("GJR GARCH").Range(Cells(2, j), Cells(1063, j + 4))
MLNDensity = Application.WorksheetFunction.VLookup(Sheets("GJR GARCH").Cells(i, 122).Value, MLNRange, 5, "True")
MLNDensity = Sheets("GJR GARCH").Cells(i, 131)
Next i
Next j
End Sub
编辑2:你有" nexti"和" nextj"这就是抛出错误的原因。应该是"接下来我"和"接下来j"
答案 0 :(得分:1)
你有多个问题。首先,
Set MLNRange = Sheets("GJR GARCH").Range(Cells(2, j).Value, Cells(1063, j + 4).Value)
.value不是范围,它是属性。只需使用单元格引用
Set MLNRange = Sheets("GJR GARCH").Range(Cells(2, j), Cells(1063, j + 4))
其次,增量变量没有正确嵌套
For i = 1 to 10
for j = 1 to 10
next i
next j
'/ should be
for i
for j
next j
next i
您可以在for语句中定义步骤。例如。
For i = 10 to 23 step 2
for j = 1 to 116 step 10
'/ Code
next j
next i
i will then go 10, 12, 14, 16 etc. and j will go 1, 11, 21, 31 etc.
而不是在任何地方引用Sheets("GJR GARCH")
(这是一个混乱和痛苦的变化)。将工作表定义为变量,然后引用它。
Dim wsData as Worksheet
wsData = Sheets("GJR GARCH")
MLNDensity = wsData.Cells(i, 125)
答案 1 :(得分:0)
.Value不属于那里:
Set MLNRange = Sheets("GJR GARCH").Range(Cells(2, j), Cells(1063, j + 4))
你在循环中增加循环变量i和j似乎很奇怪。你可以写例如。
For i = 10 To 23 Step 2