将主题颜色从数据透视表应用到相邻单元格

时间:2015-09-28 22:55:42

标签: excel vba colors themes pivot-table

我在遇到问题我要将一个单元格的颜色复制到下一个单元格,但是当它复制时,颜色总是白色或“没有填充”。 以下代码示例是问题区域:

'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

1 个答案:

答案 0 :(得分:0)

使用 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