【问题标题】:Applying Theme Colors From Pivot Table to Adjacent Cells将数据透视表中的主题颜色应用于相邻单元格
【发布时间】:2015-09-28 22:55:42
【问题描述】:

我在 中遇到问题,我想将一个单元格的颜色复制到下一个单元格,但是当它复制时,颜色始终为白色或“无填充”。 以下代码示例是问题区域:

'If the status cell('E') is empty (is not a table header)
        If rngCell.Offset(0, 2).Value = "" Then

           'Apply the color of 'C' column to the cells in the status('E') and note('F') column
            rngCell.Offset(0, 2).Interior.ThemeColor = rngCell.Interior.ThemeColor
            rngCell.Offset(0, 3).Interior.ThemeColor = rngCell.Interior.ThemeColor
        End If

我认为问题在于我试图将数据透视表中的单元格的颜色复制到数据透视表中不存在的单元格中。这就是我使用'.ThemeColor;'的原因颜色设置为“Pivot Style Medium 2”,并且该颜色无法使用 .ColorIndex。

我尝试过使用 .ColorIndex、.Color、.ThemeColor,但所有尝试都给出了相同的结果

以下代码示例是提供上下文的完整代码,但问题在于最后一个 else 语句。

'Initialize variables to save cell ranges
Dim rngTables As range
Dim rngClick As range
Dim rngCell As range

'Set the range of cells to be checked
'We want to check the description fields to check if there is description text
'Cells with descriptions are the parameters being tested
Set rngTables = range("C4:C200")

'Loop through the cells in rngTables using rngCell to check each cell individually
For Each rngCell In rngTables

    'Add cells to rngClick if they are NOT Blank
    If Not rngCell.Value = "" And Not rngCell.Value = "Description" Then
        If Not rngClick Is Nothing Then

            'Add the 2nd, 3rd, 4th... Cell to our new range, rng2
            'This is the most common outcome so place it first in the IF test (faster coding)
            Set rngClick = Union(rngClick, rngCell.Offset(0, 2))
        Else

            'The first valid cell becomes rngClick
            Set rngClick = rngCell.Offset(0, 2)
        End If
    Else

        'If the status cell('E') is empty (is not a table header)
        If rngCell.Offset(0, 2).Value = "" Then

           'Apply the color of 'C' column to the cells in the status('E') and note('F') column
            rngCell.Offset(0, 2).Interior.ThemeColor = rngCell.Interior.ThemeColor
            rngCell.Offset(0, 3).Interior.ThemeColor = rngCell.Interior.ThemeColor
        End If
    End If
Next rngCell

【问题讨论】:

    标签: excel-vba excel vba colors themes pivot-table


    【解决方案1】:

    通过使用从数据透视表中提取主题颜色的 DisplayFormat.Interior.Color 解决了该问题。

    If rngCell.Offset(0, 2).Value = "" Then
    
               'Apply the color of 'C' column to the cells in the status('E') and note('F') column
                rngCell.Offset(0, 2).Interior.Color = rngCell.DisplayFormat.Interior.Color
                rngCell.Offset(0, 3).Interior.Color = rngCell.DisplayFormat.Interior.Color
            End If
    

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 2021-03-28
      • 2018-04-11
      • 2013-10-18
      • 2017-06-26
      • 2017-09-11
      相关资源
      最近更新 更多