【问题标题】:Select a range of data from active cell to specified cell in a different column选择从活动单元格到不同列中指定单元格的数据范围
【发布时间】:2014-08-14 18:52:39
【问题描述】:

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

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

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    宏将始终选择Activecell 下方的 200 行和 52 列

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

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

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-27
        • 2019-03-14
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多