使用`With`语句的对象作为过程调用的参数
是否可以使用With
语句的对象作为从With
块中调用的过程的参数,而不必完全限定该对象? 它可以等同于this
或me
。
With thisThing.thatThing.otherThing.myObject MySub [?] ' How do I specify myObject as the parameter? MySub This 'No, that's not it... MySub Me 'Not this either... what is it? 'Of course I could do this: MySub thisThing.thatThing.otherThing.myObject 'But I'd prefer not having to fully qualify myObject like that .... End With
例如:
With Worksheet.Range("A1:E4") Call SubName(<range from with>) End With
<range from with>
将是指Worksheet.Range("A1")
编辑:
似乎我暗示了一个单一的价值范围只是一个单一的单元格范围,我的坏。 我特别试图parsing一个范围到我打电话的过程(它在指定的范围周围绘制一些边界)。
我的实际代码:
With ReportSheet // Call drawBorder(.Range(.Cells(j + 9, 2), .Cells(k + 9, 2))) <--What I have to do right now With .Range(.Cells(j + 9, 2), .Cells(k + 9, 2)) //Call drawBorder(<the specified range above> <--What I want to do //Other code End With End With Sub drawBorder(drawRange As Range) With drawRange //Various code End With End Sub
您可以使用
drawBorder .Cells
注意:不需要使用Call
,这个Sub
名字本身就是紧跟着没有包围的参数就足够了
我明白你在做什么,但不幸的是,没有办法直接做你正在问的东西。 With
语句的一个弱点正是你所指的:当你说With myObject
,你可以很容易地引用myObject
的子方法和属性,但是没有办法间接引用myObject
本身。
这可能是为什么With
语句还没有超越Visual Basic。 在其他语言中,这将是这样做的标准方式:
Dim rng as Range '... With ReportSheet '... Set rng = .Range(.Cells(j + 9, 2), .Cells(k + 9, 2)) With rng DrawBorder rng ' can also move this outside With block if you prefer .DoThis .DoThat '... End With End With
即设置一个明确的引用到你感兴趣的对象,然后使用它。