2

そのオブジェクトを完全に修飾することなく、Withステートメントのオブジェクトをブロック内から呼び出されるプロシージャのパラメーターとして使用することは可能ですか? またはWithに相当する可能性があります。thisme

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

あなたが何を得ているかはわかりますが、残念ながら、あなたが求めていることを直接行う方法はありません. このステートメントの弱点の 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 に答える