尝试使BeforeDoubleClick更改行B:S之间的行颜色并在A

时间:2016-05-24 05:29:00

标签: vba excel-vba excel

初学者在这里! 我从互联网的各种来源编造了这个代码,并试图将它们整合在一起,并没有成功。任何评论将不胜感激:))

    Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object,ByValTarget As Range, Cancel As Boolean)
If Target.Column > 1 And Target.Column < 20 Then
If Target.Interior.Color = vbWhite Then
Sh.Rows(Target.Row).Interior.Color = RGB(200, 162, 200)
ElseIf Target.Interior.Color = RGB(200, 162, 200) Then
Sh.Rows(Target.Row).Interior.Color = RGB(255, 192, 203)
ElseIf Target.Interior.Color = RGB(255, 192, 203) Then
Sh.Rows(Target.Row).Interior.Color = xlNone
End If
Cancel = True
End If
End Sub

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A7:A4608")) Is Nothing Then
Cancel = True
Target = Format(Now, "DD MMM yyyy, ttttt")
End If
End Sub

日期将始终在DoubleClick上插入,但除非我完全删除底部代码,否则行不会为我的血腥生活改变颜色

如果我没有正确粘贴代码,我也会提前道歉。如果你没有抓到,我是一个新手&lt; 1个月:)

1 个答案:

答案 0 :(得分:0)

您可以将两者结合在一起:

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, _
                           ByVal Target As Range, Cancel As Boolean)

    If Not Intersect(Target, Sh.Range("A7:A4608")) Is Nothing Then

        Target.Value = Format(Now, "DD MMM yyyy, ttttt")
        Cancel = True

    ElseIf Target.Column > 1 And Target.Column < 20 Then

        If Target.Interior.Color = vbWhite Then
            Sh.Rows(Target.Row).Interior.Color = RGB(200, 162, 200)
        ElseIf Target.Interior.Color = RGB(200, 162, 200) Then
            Sh.Rows(Target.Row).Interior.Color = RGB(255, 192, 203)
        ElseIf Target.Interior.Color = RGB(255, 192, 203) Then
            Sh.Rows(Target.Row).Interior.Color = xlNone
        End If
        Cancel = True

    End If
End Sub