动态范围浓缩为一行代码

时间:2016-05-24 17:53:52

标签: excel vba excel-vba

我正在试图找出创建动态范围的最简洁方法,我认为这与我想要它做的很接近,但我不确定如何使其正确。有什么想法吗?

Sub Macro1()
Dim RNG As Range
With Sheets("Open Jobs Report") 'Change to your sheet
Set RNG = .Range("A1", .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column))
End With

2 个答案:

答案 0 :(得分:3)

试试这个:

Dim RNG As Range
Set RNG = .Range("A1", .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column))

因为您在某些范围对象前面使用.,我认为它位于With Block中,如下所示:

Sub foooooii()
Dim RNG As Range
With Sheets("Sheet3") 'Change to your sheet
    Set RNG = .Range("A1", .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column))
End With
Debug.Print RNG.Address
End Sub

答案 1 :(得分:3)

建立在斯科特的解决方案上,只是为了缩短它:

Set RNG = .Range(.Cells(.Rows.Count, "A").End(xlUp), .Cells(1, .Columns.Count).End(xlToLeft))