【问题标题】:dates, columns, conditional formatting, neighbouring cells (below/above)日期、列、条件格式、相邻单元格(下方/上方)
【发布时间】:2017-11-26 11:56:15
【问题描述】:

大家好,

我确实相信我的问题需要 VBA 代码。 所以,请善待并帮助我。

我有很多日期的表格(所有日期都在一个特定的列中)。 带有日期的单元格无需特殊顺序即可填充。 由于其他数据(其他列中的数据),过滤不适用。

需要什么(问题):

  • 有没有办法比较相邻单元格的日期(单元格上方/下方),

  • 如果日期相同,则从具有相同日期的列中的 1 个单元格中复制条件格式,

  • 仅当相邻日期不同时,条件格式才会更改,

例如:

Column (date)   I conditional formatting interior cell color

A           I   (CF)

25.11.2017  I   blue

26.11.2017  I   red

26.11.2017  I   red

26.11.2017  I   red

22.11.2017  I   blue

22.11.2017  I   blue

25.11.2017  I   red

etc.        I   etc.

我熟悉 VBA,所以 VBA 代码会很棒。我相信,这里需要 Private Sub。

非常感谢。

来自克罗地亚的问候

马尔科

【问题讨论】:

  • 实际来源列之间是否有空格?
  • 单元格中没有空格。列中可能有空单元格
  • 请详细说明上色标准。
  • 着色标准 - 这是我需要您帮助的主要部分: - 更改日期的单元格(与上面的单元格相比)需要以特定颜色(例如:红色)着色,例如 - 单元格A2 (26.11.2017) 与上面的单元格比较 A1 (24.11,2017) 将单元格 A2 内部变为红色 - 下一个日期与上面的单元格比较(偏移量 (-1,0)) 示例单元格 A3 (26.11.2017) 与单元格比较A2 以上(26.11.2017)将单元格 A3 内部与 A2 内部相同,
  • - 下一个日期/单元格 A4 (30.11.2017) 与 A3 上方的单元格 (26.11.2017) 将单元格 A4 内部变为蓝色 - 单元格 A5 (30.11.2017) 与 A4 上方的单元格相比 ( 30.11.2017) 将单元格 A% 内部与 A4 内部相同,- .... - 日期列中可能有空白单元格,空白单元格的格式无关紧要(可以着色为红色、蓝色或无颜色) 日期范围非常大(2007 年至现在(未来)之间的任何日期)。

标签: vba excel date


【解决方案1】:

假设数据从单元格 A1 开始。我认为这可能可以简化一点。

然而,

Public Sub ColourCells()

   Dim wb As Workbook
   Dim wsSource As Worksheet

   Set wb = ThisWorkbook
   Set wsSource = wb.Worksheets("Sheet1")

   Dim lastRow As Long

   lastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row

   Dim loopRange As Range

   Set loopRange = wsSource.Range("A1:A" & lastRow) ' adjust here if starts elsewhere

   Dim currValue As Variant
   Dim cellColor As String

   cellColor = vbBlue

   Dim currCell As Range

   For Each currCell In loopRange.Rows

       If Not IsEmpty(currCell) Then

           If currCell.Row = 1 Then 
                currCell.Font.Color = cellColor
                currValue = currCell.Value2

           ElseIf currCell = currValue Then
                currCell.Font.Color = cellColor

           Else
                If cellColor = vbBlue Then
                    cellColor = vbRed
                Else  
                    cellColor = vbBlue
                End If

                currCell.Font.Color = cellColor
                currValue = currCell.Value2

          End If

       End If

   Next currCell

End sub

【讨论】:

  • QHarr 一见到你,我就请你喝啤酒。你的程序效果很好。这很简单,最重要的是完成这项工作。再一次,非常感谢。问候,马尔科
  • 很高兴能提供帮助。
猜你喜欢
  • 2020-04-13
  • 2012-12-24
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
相关资源
最近更新 更多