从范围对象引用范围

时间:2016-01-09 07:57:26

标签: excel vba excel-vba

我试图找到一种方法来引用另一个范围的范围,例如, 保持单元格的范围" 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 个答案:

答案 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
相关问题