从活动单元格到不同列中的特定单元格选择一系列数据

时间:2014-08-14 18:52:39

标签: excel vba excel-vba

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

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

2 个答案:

答案 0 :(得分:0)

宏将始终在Activecell下方选择200行,在

中选择52列

你可以明显改变参数(即200,52)

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

答案 1 :(得分:0)

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

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