为单元格设置颜色和图案

问题描述 投票:0回答:1

我使用下面的代码创建了一个 BlinkCell 效果

Sub BlinkCell(cel As Range)
    ' Capture the original cell formatting
    Dim originalColor As Long
    originalColor = cel.Interior.Color

    Dim originalPattern As Long
    originalPattern = cel.Interior.Pattern

    ' Blink the cell normally
    Dim i As Integer
    For i = 1 To 3
        ' Change the background color to white
        cel.Interior.Color = RGB(255, 255, 255)

        ' Remove any existing pattern
        cel.Interior.Pattern = xlNone

        ' Wait for 50 milliseconds
        Sleep 50

        ' Restore the original background color
        cel.Interior.Color = originalColor

        ' Restore the original pattern
        cel.Interior.Pattern = originalPattern

        ' Wait for 50 milliseconds
        Sleep 50
    Next i
End Sub

它工作正常 - 变成白色并尝试恢复原始颜色和图案 - 但不要恢复包含填充图案的单元格上的原始颜色 - 它们会变成蓝色

如何用原来的颜色和图案填充单元格而不是变成蓝色?

我也尝试过:

Sub BlinkCell(cel As Range)
  ' Capture the original cell formatting
  Dim originalColor As Long
  Dim hasFill As Boolean

  hasFill = CBool(cel.HasFill)

  If hasFill Then
    originalFill = cel.Fill.PatternStyle
  Else
    originalColor = cel.Interior.Color

  End If

  ' Blink the cell normally
  Dim i As Integer
  For i = 1 To 3
    If hasFill Then
      cel.Fill.PatternStyle = xlPatternNone ' Set fill to none
    Else
      cel.Interior.Color = RGB(255, 255, 255) ' Change background to white
    End If

    Sleep 50 ' Wait for 50 milliseconds

    If hasFill Then
      cel.Fill.PatternStyle = originalFill ' Restore original fill pattern
    Else
      cel.Interior.Color = originalColor ' Restore original background color
    End If

    Sleep 50 ' Wait for 50 milliseconds
  Next i
End Sub

代码导致以下错误:

对象不支持此属性或方法

originalFill = cel.Fill.PatternStyle

我感谢任何帮助

excel vba
1个回答
0
投票

看起来您需要使用

.Interior.Pattern
.Interior.PatternColor
来处理具有模式的单元格:

Sub BlinkCell(cel As Range)
    ' Capture the original cell formatting
    Dim originalColor As Long
    originalColor = cel.Interior.Color

    Dim originalPattern As Long
    originalPattern = cel.Interior.Pattern
    
    Dim originalPatternColor As Long
    originalPatternColor = cel.Interior.PatternColor
    
    Debug.Print originalPattern

    ' Blink the cell normally
    Dim i As Integer
    For i = 1 To 3
        ' Change the background color to white
        cel.Interior.Color = RGB(255, 255, 255)

        ' Remove any existing pattern
        cel.Interior.Pattern = xlNone

        ' Wait for 50 milliseconds
        Sleep 50

        If originalPatternColor = 0 Then
            ' Restore the original background color
            cel.Interior.Color = originalColor
        Else
            ' Restore the original pattern
            cel.Interior.Pattern = originalPattern
        
            ' Restore the original pattern color
            cel.Interior.PatternColor = originalPatternColor
        End If
        ' Wait for 50 milliseconds
       Sleep 50
    Next i
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.