2016-01-09 57 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