Excel vba 将透视表中的主题颜色应用于相邻单元格
我想将一个单元格的颜色复制到下一个单元格时遇到问题,但当它复制时,颜色总是白色或“无填充” 以下代码示例是问题区域:Excel vba 将透视表中的主题颜色应用于相邻单元格,excel-vba,excel,vba,colors,themes,pivot-table,Excel Vba,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('
'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
通过使用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