Excel 2010 VB脚本 - 突出显示行问题

时间:2013-06-09 02:44:26

标签: vb.net excel row highlight

我想知道是否有人对此有任何建议。当单击一个单元格时,我希望该行突出显示在第6行下方。因此,如果我点击A7,那么第7行将突出显示。如果我然后单击B9,第7行将删除突出显示,然后第9行将突出显示。我确实找到了能够满足我需要的代码,并对其进行了一些定制。除了Excel保存,关闭和重新打开时,一切都按照我需要的方式工作。

如果突出显示第9行,并且电子表格已保存,关闭并重新打开,则第9行将保持突出显示(即使单击其他单元格也是如此)。所以现在我突出显示了2行。为了解决这个问题,一旦打开电子表格,就要点击另一行,然后点击第9行。然后它将返回到1个突出显示的行。

有人有解决方案吗?以下是我正在使用的代码。

感谢有人提供的任何帮助,

克里斯

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

ActiveSheet.Unprotect

Static rr

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

r = Selection.Row rr = r

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

ActiveSheet.Protect

End Sub

4 个答案:

答案 0 :(得分:1)

以下代码组合似乎正在起作用;我每次都会突出整行。

Private lastRow As Long

Private Sub Worksheet_Activate()
    lastRow = ActiveCell.Row
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If lastRow <> 0 Then
        Rows(lastRow).EntireRow.Interior.ColorIndex = xlNone
        If Target.Row > 6 Then
            Target.Rows(1).EntireRow.Interior.ColorIndex = 20
        End If
        lastRow = Target.Row
    Else
        lastRow = Target.Row
    End If
End Sub

实际上,它可能需要一些工作。但是,它可能是你的起点。

答案 1 :(得分:0)

您的静态rr变量是Variant,默认值不是“”。因此,当您重新打开文件时,光标将位于之前的行中,并且因为rr不等于“”,所以它不会从该行中删除突出显示。 (事实上​​,我不确定它是如何删除当前的亮点。)

无论如何,试试:

Static rr
If IsEmpty(rr) Then
    rr = ""
End If

或者,给rr数据类型为Integer或Long,它将采用默认值0。

答案 2 :(得分:0)

我编写了自己的代码,而不是尝试使用我找到的代码。这样做效果更好。它还允许用户指定自己的行范围以突出显示。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Application.EnableEvents = False
ActiveSheet.Unprotect

Dim iFirstCol As Integer
Dim iLastCol As Integer
Dim iFirstRow As Integer
Dim iLastRow As Integer
Dim iColor As Integer

'''Only adjust the below numbers to fit your desired results.'''
iFirstCol = 1 'Change this number to the number of the first column that needs to be highlighted. Column A = 1.
iLastCol = 15 'Change this number to the number of the last column that needs to be highlighted. Column A = 1.
iFirstRow = 7 'Change this number to the number of the first row that needs to be highlighted.
iLastRow = 500 'Change this number to the number of the last row that needs to be highlighted.
iColor = 20 'Change this number to use a different highlight color.
'''End of changes, do not change anything else.'''

'The row highlight will only be applied if the selected range is within this if statement criteria.
If Target.Row > iFirstRow - 1 And Target.Row < iLastRow + 1 And Target.Column > iFirstCol - 1 And Target.Column < iLastCol + 1 Then

    'Resets the color within the full range when cell selection changed.
    ActiveSheet.Range(ActiveSheet.Cells(iFirstRow, iFirstCol), ActiveSheet.Cells(iLastRow, iLastCol)).Interior.Color = xlNone

    'Applies the colors to the row.
    For counter = iFirstCol To iLastCol
        With ActiveSheet.Cells(Target.Row, iFirstCol).Interior
            .ColorIndex = iColor
            .Pattern = xlSolid
        End With
        iFirstCol = iFirstCol + 1
    Next counter

End If

ActiveSheet.Protect
Application.EnableEvents = True

End Sub

答案 3 :(得分:0)

我经常在选择中突出显示表格中的行。虽然我可能过度简化了事情,但它似乎比上面提供的代码容易得多。 这就是我的所作所为; 我只在工作表选择更改中使用了一小部分代码,该范围应该有突出显示的行,例如:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("D8:R10000")) Is Nothing Then
Range("B1").Value = ActiveCell.Row
End If
End Sub

然后我对B1和范围使用条件格式,使用您可能想要的所选行的任何格式。上面的条件格式化公式将是: = $ B $ 1 = ROW() 应用范围:= $ D $ 8:$ R $ 10000

就是这样。不需要其他编码,格式可以简单地更改。 你对此有何看法?