繁体   English   中英

在其他列中选择从活动单元格到指定单元格的数据范围

[英]Select a range of data from active cell to specified cell in a different column

如何选择一个范围,该范围包含从活动单元格(或行)下方的行到另一列中指定单元格的数据?

例如,如果选择了第7行(或单元格A7),我想选择从A8到BA200的所有单元格(或行)。

宏将始终在Activecell下选择200行,并在整个范围内选择52列

您显然可以更改参数(即200,52)

Sub Picker()
 Range(ActiveCell.Offset(200 - ActiveCell.Row, 0), ActiveCell.Offset(1, 52)).Select
End Sub

如果它们包含数据(或所需的任何其他条件),则此代码将选择相对于起始单元格在指定范围内的所有单元格。

Private Sub selectRangeWithData()

Dim startCell As Range
Dim dataRange As Range
Dim rowSpan As Integer, colSpan As Integer

Set startCell = Range("A7")
rowSpan = 200
colSpan = 52

'initialize the first cell of the selected range
Set dataRange = Range(startCell.Offset(1, 0).Address)

'loop through the supplied range and determine if data exists
For Each cell In Range(startCell.Offset(1, 0), startCell.Offset(rowSpan, colSpan)).Cells
    If cell <> "" Then
        'if so, add it to the range
        Set dataRange = Union(dataRange, Range(cell.Address))
    End If
Next cell
'finally, select the range of cells
dataRange.Select

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM