执行以下脚本时,我无法摆脱run time error 9
。
我想要做的是搜索工作表2中的特定单元格值,并将其放在工作表1中的一系列单元格中。
这是我的剧本:
Public output() As Variant
Sub james()
Dim J As Object
Dim c As Object
Sheet2.Activate
ReDim ouput(3)
Set J = Cells(1, 1)
output(1) = J.Offset(0, 5).Value
output(2) = J.Offset(30, 5).Value
output(3) = J.Offset(60, 5).Value
Sheet1.Activate
Range("B7:B86").Select
For Each c In Selection
If c.Value = "output(1)" Then
Exit For
End If
Next c
Rows(c.Row).Select
End Sub
答案 0 :(得分:0)
@ SearchAndResQ是对的,如果你希望你的数组从1开始,你有两个选择:
在模块的开头使用Option Base 1
,或Redim output(1 to 3)
此外,您还需要更改If
语句:
If c.Value = output(1) then
总而言之,这是您的代码的更好版本:
Public output() As Variant
Sub james()
Dim J As Range
Dim c As Range
ReDim ouput(1 to 3)
Set J = Sheet2.Cells(1, 1)
output(1) = J.Offset(0, 5).Value
output(2) = J.Offset(30, 5).Value
output(3) = J.Offset(60, 5).Value
For Each c In Sheet1.Range("B7:B86")
If c.Value = output(1) Then
Exit For
End If
Next c
Rows(c.Row).Select
End Sub