[英]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.