【问题标题】:Selecting Multiple Rows in Excel在 Excel 中选择多行
【发布时间】:2016-03-24 03:05:54
【问题描述】:

我使用一个大型跟踪器来存储网站的建成日期。我正在尝试查看一系列单元格并确定日期是否是今天的日期。在我的跟踪器中实施之前,我写了一个小 Excel 表以使其不那么复杂。

这就是我正在使用的。

问题是当有更多时,它只选择今天日期的一行。

这是 Excel 在按下按钮时所做的选择。

如何选择多行? (它们可能不相邻。将来可能是第 1 行、第 5 行和第 8 行)。

'   Date Selection Macro
'   Goal! - Bring automated cell selection based on date range
'   TODO - Create macro to select rows that have a given date range
'   TODO - Create a window that allows you to add the given date range 
'   rather than hard code
'   TODO - Export selected rows to an email template in Outlook

Sub DateSelection()
    Dim completionDate As Range
    Set completionDate = Range("B2:B8")

    Dim todaysDate As Date
    todaysDate = Date

    For Each cell In completionDate
        If cell = todaysDate Then
            cell.EntireRow.Select
        Else
            'cell.Font.ColorIndex = 3
        End If
    Next
End Sub

【问题讨论】:

    标签: vba excel date


    【解决方案1】:

    你需要像这样的Union

    Sub DateSelection()
    
      Dim completionDate As Range
      Set completionDate = Range("B2:B8")
      Dim rng As Range
      Dim todaysDate As Date
      todaysDate = Date
    
      For Each cell In completionDate
    
          If cell = todaysDate Then
              If rng Is Nothing Then
                Set rng = cell.EntireRow
              Else
                Set rng = Union(rng, cell.EntireRow)
              End If
          Else
              'cell.Font.ColorIndex = 3
    
      End If
    
      Next
      rng.Select
    End Sub
    

    但是,您也可以使用过滤器仅显示具有今天日期的行,然后选择所有显示的行。也会这样做:)

    【讨论】:

    • 这就像一个魅力德克!我只是想了解如何大声笑。为什么要检查范围是否为空?再次,请原谅基本问题哈哈。
    • 只是为了避免错误。如果rng 为空,则union 将失败并弹出错误消息:)
    • 谢谢德克!非常感谢先生!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多