简体   繁体   中英

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

How do you select a range that contains data from the row below an active cell (or row) to a specified cell in a different column?

For example, if row 7 is selected (or cell A7), I want to select all of the cells (or rows) with data from A8 to BA200.

The Macro will always select 200 rows below the Activecell and 52 columns across

You can obviously change the parameters(ie 200,52)

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

This code will select all the cells in the specified span relative to the starting cell if they contain data (or any other criteria you want).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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