Excel 2010的VB脚本 - 高亮行发行(Excel 2010 VBScript – High

2019-10-29 05:08发布

我想知道,如果有人有这个任何建议。 我想该行突出低于6行被点击了一个单元格时。 所以,如果我点击A7,那么第7行会突出。 如果我再点击B9,第7行会的亮点删除,然后第9行会突出。 我发现代码,并为我所需要的工作,并已经定制了一点。 一切工作正是我需要它的工作,除了当Excel保存的方式,收出,并重新打开。

如果列9高亮显示,电子表格保存,关闭,并重新打开,一排9将保持突出(即使点击另一个单元格)。 所以现在我有两行加亮。 为了一旦打开电子表格备份是点击不同的行,然后单击重新排9.然后,它会回到1突出显示的行解决此问题。

有没有人有一个解决方案? 下面是我使用的代码。

感谢您的帮助,有人可以提供,

克里斯

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

ActiveSheet.Unprotect

静态RR

如果RR <> “”,则与行(RR).Interior .ColorIndex = xlNone结束结束如果

R = Selection.Row RR = R

与行(R).Interior .ColorIndex = 20 .Pattern = xlSolid一端与

个ActiveSheet.Protect

结束小组

Answer 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

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



Answer 2:

您的静态变量RR是一个变量,不会有“”的默认值。 所以,当你再次打开该文件时,光标会行这是在以前,因为RR不等于“”它不会从该行删除亮点。 (事实上​​,我不知道它是如何消除目前的亮点。)

总之,请尝试:

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

可替代地,给RR数据型整型或长的,这将承担的默认值0。



Answer 3:

我写的,而不是试图用我发现代码工作我自己的代码。 这工作好了很多。 它也允许用户指定自己的行范围,以突出。

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


Answer 4:

我经常强调在选择表中的行。 虽然我可能过于简化的东西似乎容易得多,那么你在上面提供的代码。 这是我做的; 我用在为已经生效的高亮行,像这样的范围内的工作表选择更改代码只是一个很小的大:

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()用Applied To范围的:= $ D $ 8:$ R $ 10000

而已。 没有其他的编码要求和格式可以简单地改变。 你对这个想法?



文章来源: Excel 2010 VB Script – Highlight Row Issue