代码导致1004错误,我不确定为什么

时间:2018-08-10 21:06:51

标签: excel-vba excel-2010 syntax-highlighting

所以我有这串代码,应该让我自动突出显示活动单元格的行和列...问题是它弹出了1004错误,我不确定为什么。可能在语法上吗?

它标记“ .colorindex = 20”

我正在运行excel 2010

Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static rr
Static cc

If cc <> "" Then
    With Columns(cc).Interior
        .ColorIndex = xlNone
    End With
    With Rows(rr).Interior
        .ColorIndex = xlNone
    End With
End If

r = Selection.Row
c = Selection.Column
rr = r
cc = c

With Columns(c).Interior
    .ColorIndex = 20
    .Pattern = xlSolid
End With
With Rows(r).Interior
    .ColorIndex = 20
    .Pattern = xlSolid
End With
End Sub

2 个答案:

答案 0 :(得分:0)

您必须添加:

Dim r, c

代码:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static rr
Static cc
Dim r, c

If cc <> "" Then
    With Columns(cc).Interior
        .ColorIndex = xlNone
    End With
    With Rows(rr).Interior
        .ColorIndex = xlNone
    End With
End If

r = Selection.Row
c = Selection.Column
rr = r
cc = c

With Columns(c).Interior
    .ColorIndex = 20
    .Pattern = xlSolid
End With
With Rows(r).Interior
    .ColorIndex = 20
    .Pattern = xlSolid
End With
End Sub

enter image description here

答案 1 :(得分:0)

已解决...必须以这种格式编辑颜色。然后她跑了...我唯一要注意的是现在正在重新格式化我已经在纸上使用的颜色。

从原始代码到突出显示的地方都可以解决吗?

With Columns(c).Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 10937227
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
With Rows(r).Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 10937227
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
End Sub