合并单元格的代码错误

时间:2015-10-05 19:19:37

标签: excel excel-vba merge vba

我是新手,需要帮助解决这个问题。我试图在excel上使用此代码来更改单元格的颜色和文本,但它不适用于合并的单元格。如何使它适用于合并的单元格?

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Intersect(Target, Range("$C$17:$C$80")) Is Nothing Then Exit Sub
    Select Case Target
    Case ""
        Target = "Priority 1"
        Target.Interior.ColorIndex = 3
    Case "Priority 1"
        Target = "Priority 2"
        Target.Interior.ColorIndex = 6
    Case "Priority 2"
        Target = "Priority 3"
        Target.Interior.ColorIndex = 45
    Case Else
        Target = ""
        Target.Interior.ColorIndex = 15
    End Select
    Cancel = True
End Sub

1 个答案:

答案 0 :(得分:2)

超级关闭,只需更改一行:Select Case TargetSelect Case Target.Item(1)

完整代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Intersect(Target, Range("$C$17:$C$80")) Is Nothing Then Exit Sub

    Select Case Target.Item(1)
    Case ""
        Target = "Priority 1"
        Target.Interior.ColorIndex = 3
    Case "Priority 1"
        Target = "Priority 2"
        Target.Interior.ColorIndex = 6
    Case "Priority 2"
        Target = "Priority 3"
        Target.Interior.ColorIndex = 45
    Case Else
        Target = ""
        Target.Interior.ColorIndex = 15
    End Select
    Cancel = True
End Sub

结果:

你可以在这里看到我有一个从C23到C26的合并区域

enter image description here