2009-01-20 52 views
1

我正在嘗試這個,但它給我一個類型不匹配。有沒有另一種方法來做到這一點?我假設rSource不是來自活動工作表。如何複製vba中的範圍?

另外,我只需要值。

編輯:rSource預計有行和列。它也將是一個連續的區域。

編輯:一旦此操作完成,我應該能夠獨立篩選每個範圍。

Function CopyRange(rSource As range) As range 
    ' Declarations 
    Dim rTemp As range 

    ' Create new range starting at cell A1 
    Set rTemp = Range(Cells(1, 1), Cells(rSource.Rows.Count, rSource.Columns.Count)) 

    rTemp.Value = rSource.Value 

    Set CopyRange = rTemp 
End Function 

回答

0

這是連續區域嗎?它是否由空單元格組成?如果是這樣,你應該能夠使用currentregion屬性來設置範圍。

我想你得到一個錯誤的原因是因爲你沒有在你的第一個單元格聲明中設置rSource。

1

我相信你的功能沒問題。

我創建的testStub:

Function Check() 
    Dim x As Range 
    Dim c As Range 
    Set c = Range("A1", "A2") 
    Set x = CopyRange(c) 
End Function 

隨着單元A1設置爲2和A2設置爲3。

在踏入此,和評估使用x變量:
X?。範圍( 「A1」)
(我有型?X像你一樣不匹配。)

我得到2

希望有所幫助。

1

假設這段代碼在工作表的代碼模塊中,那麼它應該工作得很好。我只是試了一下,它運行良好(假設傳遞給函數的範圍是連續的)。

另外,試試這個: -

Function CopyRange(rSource As Range) As Range 

    Call rSource.Copy(Cells(1, 1)) 

    Set CopyRange = Range(rSource.Address).Offset(1 - rSource.Row, 1 - rSource.Column) 

End Function 
+0

此副本的範圍內,但他們似乎被捆綁在一起。過濾一個過濾另一個。 – 2009-01-20 17:12:28