根据另一个单元格的值定义范围 [英] Define range based on the value of another cell
问题描述
是否可以根据单元格中给出的值定义VBA中的范围?
Is it possible to define a range in VBA based on a value given in a cell?
例如,我有一个数据集有四列和不断变化的行数。我有单元格 F2
中的行数。假设单元格 F2
表示行数 385
,那么我可以 Range A1:D385)。选择
,但是我希望最后一个单元格的选择依赖于单元格 F2
。
For example, I have a dataset with four columns and a constantly changing number of rows. I have the number of rows in cell F2
. Suppose cell F2
indicates the number of rows is 385
, then I be able to Range("A1:D385").Select
, but I want the selection of the last cell to be dependent on cell F2
.
所以如果我将 F2
更改为 50
,那么下一次运行宏时,将会选择 A1:D50
,但由于我是VBA新手,我无法理解。
So if I change F2
to 50
, that the next time I run the macro, A1:D50
will be selected, but since I'm new to VBA I can't figure it out.
推荐答案
这是你正在寻找的:
Dim lastRow As Integer
With ThisWorkbook.Sheets(1)
lastRow = .Cells(.Rows.Count, "F").End(xlUp).row
.range("A1:D" & lastRow).Select
End With
这篇关于根据另一个单元格的值定义范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!