简体   繁体   English

Excel VBA-突出显示所选单元格

[英]Excel VBA - Highlight Selected Cell

I am using this code to highlight the selected cell and it works fine. 我正在使用此代码突出显示所选的单元格,并且工作正常。 However, I was wondering if there is a better way of doing it without using On error resume next . 但是,我想知道是否有一种更好的方法,而不必使用On error resume next

Also, If I use this statement does that mean other errors in the same event or procedures called by the event would not be catched? 另外,如果我使用此语句,是否意味着同一事件中的其他错误或该事件调用的过程将不会被捕获?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Union(Me.Range("range_name"), Me.Range("range_name2"), _
Me.Range("range_name3"))) Is Nothing Then
Static xLastRng As Range
On Error Resume Next
Target.Interior.ColorIndex = 6
xLastRng.Interior.ColorIndex = xlColorIndexNone
Set xLastRng = Target
End If
End Sub

Here is another approach, because right now you are inputing a fill color instead of conditional formatting. 这是另一种方法,因为现在您正在输入填充颜色而不是条件格式。 You might ruin other cells their format doing so. 您可能会破坏其他单元格的格式。

What I done is for example use this conditional formatting rule on column C, D and E (you have other ranges so use them accordingly). 例如,我所做的就是在C,D和E列上使用此条件格式设置规则(您还有其他范围,因此请相应地使用它们)。

=AND(ROW()=CELL("ROW"),COLUMN()=CELL("COLUMN"))

This alone should do the trick, but it's some kind of glitch (too fast) for the screen to properly update the selected cell with a conditional format. 仅此一项就可以解决问题,但是屏幕出现某种故障(过快),无法以条件格式正确更新所选单元格。 Scrolling down and back up fixes this and you will see that the selected cell is formatted if it is within your ranges. 向下滚动和向上滚动可解决此问题,如果选定的单元格在范围内,则会看到该单元格已格式化。

To counter this I used a forced waiting time on a selection change in the worksheet untill Excel is done calculating... 为了解决这个问题,我在工作表中的选择更改上使用了强制等待时间,直到Excel完成计算为止。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Application.ScreenUpdating = False
If Not Application.CalculationState = xlDone Then
    DoEvents
End If
Application.ScreenUpdating = True

End Sub

No you will notice that it will not glitch out :) 不,您会注意到它不会毛刺:)

在此处输入图片说明

If the glitch doesn't happen on your side, you can leave out the VBA part. 如果没有出现毛刺,可以忽略VBA部分。

Try this: 尝试这个:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Static xLastRng As Range
    Dim rng As Range
    Set rng = Application.Intersect(Target, Union(Me.Range("range_name"), _
                                                  Me.Range("range_name2"), _
                                                  Me.Range("range_name3")))
    'clear previous range hilite first, since overlap
    '  between previous & new could occur
    If Not xLastRange Is Nothing Then 
        xLastRng.Interior.ColorIndex = xlColorIndexNone
        Set xLastRange = Nothing
    End If

    If Not rng Is Nothing Then
        Target.Interior.ColorIndex = 6
        Set xLastRange = rng
    End If 
End Sub

It's unclear from your question whether you'd want to clear any previous highlighting if a new selection falls outside of your checked ranges. 从您的问题尚不清楚,如果新选择超出了选中的范围,是否要清除以前的突出显示。

I like this one!! 我喜欢这一个!!

http://www.cpearson.com/excel/RowLiner.htm http://www.cpearson.com/excel/RowLiner.htm

在此处输入图片说明

Simply point to the Excel AddIn and run it. 只需指向Excel加载项并运行它。

https://trumpexcel.com/excel-add-in/ https://trumpexcel.com/excel-add-in/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM