2013-10-31 64 views
1

是否可以使用With語句的對象作爲從With塊中調用的過程的參數,而無需完全限定該對象?它可能相當於thisme將`With`語句的對象用作過程調用的參數

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 
+0

+1我問了自己同樣的問題無數次。雖然我害怕[我只對你有壞消息](http://stackoverflow.com/a/19700906/119775)。 –

回答

2

您可以使用

drawBorder .Cells 

注:沒有必要使用Call,在Sub名自身其次是加括號的參數足以

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 

即設定一個明確提及您感興趣的對象,並使用該此後。

相關問題