在Excel中选择多行

时间:2015-12-17 15:21:18

标签: excel vba excel-vba date

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

这就是我正在使用的。

Excel Screenshot

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

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

Excel Selection

如何选择多行? (它们可能不会彼此相邻。将来可能是第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

1 个答案:

答案 0 :(得分:3)

你需要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

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