使用VBA声明相对于活动单元格的范围 [英] Declare a Range relative to the Active Cell with VBA

查看:128
本文介绍了使用VBA声明相对于活动单元格的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要声明一个相对于活动单元格的范围对象。问题是,每次宏运行时,我想选择的行数和列数不同。

I need to declare a range object relative to the Active Cell. The problem is, the number of rows and columns I want to select is different each time the macro runs.

例如,我有两个变量: numRows numCols

For example, I have two variables: numRows and numCols.

我想选择一个有ActiveCell在左上角的手中,单元格的行ActiveCell.Row + NumRows和列ActiveCell.Column + NumCols在右下角(然后我打算将此数据复制到一个数组加快我的宏)。

I want to select a range that has the ActiveCell in the upper-left corner hand has the cell with row ActiveCell.Row + NumRows and column ActiveCell.Column + NumCols in the bottom right (and then I intend to copy this data to an array to speed up my macro).

有关如何做的任何建议?

Any suggestions on how to do this?

推荐答案

有一个 .Offset 属性在一个Range类上,允许你做你所需要的

There is an .Offset property on a Range class which allows you to do just what you need

ActiveCell.Offset(numRows,numCols)

跟进评论:

Dim newRange as Range
Set newRange = Range(ActiveCell, ActiveCell.Offset(numRows, numCols))

,您可以通过 MsgBox进行验证newRange.Address

这里是如何将此范围分配给数组

这篇关于使用VBA声明相对于活动单元格的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆