2

是否可以将With语句的对象用作从块内调用的过程的参数With,而不必完全限定该对象?它可能相当于 athisme

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")

编辑:

似乎我通过给出一个只有一个单元格范围的范围来暗示一个值,我的错。我特别试图将一个范围解析为我正在调用的过程(它在指定范围周围绘制了一些边框)。

我的实际代码:

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
4

2 回答 2

2

您可以使用

drawBorder .Cells

注意:不需要使用CallSub自己的名字后跟不带括号的参数就足够了

于 2013-10-31T06:19:23.387 回答
1

我明白你在做什么,但不幸的是,没有办法直接做你所要求的。该语句的一个弱点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

即设置对您感兴趣的对象的显式引用,然后使用它。

于 2013-10-31T07:50:11.440 回答