考虑这个 MWE:
Option Explicit
Sub test()
Dim r1, r2 As Range
Set r1 = Worksheets("MySheet").Range("MyNamedRange")
Set r2 = NewRange(r1)
End Sub
Function NewRange(rng As Range) As Range
Set NewRange = Offset(rng, 1, 1)
End Function
当我运行
test()
时,它显示“ByRef 参数类型不匹配”,并突出显示该行中的 r1
Set r2 = NewRange(r1)
我做错了什么?
您错过了 r1 的类型定义:
Dim r1 As Range, r2 As Range
这样
Dim r1, r2 As Range
它被声明为变体。