Setting cell (n) Color Index equal to cell (n - 1) Color Index

后端 未结 1 1043
迷失自我
迷失自我 2021-01-23 16:42

I am getting a weird output from this macro. The macro should fill the blank cells with the color above creating a block of colors. The result is not what I expected even though

相关标签:
1条回答
  • 2021-01-23 17:29

    The varying colors is due to the nature of ColorIndex, which as MSDN notes represents the index of a color in a palette. Colors from different palettes will have the same index number, so use Color instead of ColorIndex.

    Change ColorIndex to Color within the innermost If...End IF.

    Sub Crayon()
    
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
    Dim MyCell As Range
    
    For Each MyCell In ws.Range("A2:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
        If MyCell.Interior.ColorIndex = xlNone Then
            If MyCell.Offset(-1).Interior.ColorIndex <> xlNone Then
                MyCell.Interior.Color = MyCell.Offset(-1).Interior.Color
            End If
        End If
    Next MyCell
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题