2016-01-09 55 views
0

我試圖找到一種方法來引用另一個範圍的範圍,例如, 保存單元格「A5:A10」的範圍中有6個單元格在該範圍內。所需要的是它旁邊的範圍是「B5:B10」。當已經有一個範圍對象(在這種情況下爲「A5:A10」)時,我可以將它引用到下一個範圍。從範圍對象中引用一個範圍

Dim R As Range 
    Dim A As Range 
    Set R = R("A5:A10").Select 
    Set R = 
'Code to refer to next column is here 

很抱歉,這可能是錯誤的語法用,因爲我在VBA編碼,它已經有一段時間開始,它只是爲了澄清什麼,需要解決這個

+1

你可以使用.offset –

+0

它給我的範圍相同的大小(範圍內的細胞數量相同)嗎? – Kozero

+0

是的,它會按照您指定的量移動您的範圍 –

回答

1

試試這個:

Sub setRanges() 
Dim ws As Worksheet 
Dim rngA As Range 
Dim rngB As Range 

'set the worksheet -- Adjust the worksheet name as required 
Set ws = ThisWorkbook.Worksheets("Sheet1") 
'set the first range to a range in the worksheet 
Set rngA = ws.Range("A5:A10") 
' set the second range to an offest of the first range 
' in this case, use an offset of one column, with the same row 
' ... remember the offset command takes rows in the first parameter 
' ... and the second parameter is for the columns 
Set rngB = rngA.Offset(0, 1) 
' so, zero row offset, i.e. stay in the same row 
' and 1 column offset to get the rngB for one column to the right of rngA 
rngB.Select 
' don't use Select in your code. This is just to demo. 

End Sub