【问题标题】:Excel 2010 VB Script – Highlight Row IssueExcel 2010 VB 脚本 – 突出显示行问题
【发布时间】:2013-06-05 01:10:56
【问题描述】:

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

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

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

感谢您提供的任何帮助,

克里斯

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

ActiveSheet.Unprotect

静态 rr

如果 rr "" 那么 带行(rr)。内部 .ColorIndex = xlNone 结束于 结束如果

r = Selection.Row rr = r

With Rows(r).Interior .ColorIndex = 20 .Pattern = xlSolid 结束于

ActiveSheet.Protect

结束子

【问题讨论】:

    标签: vb.net excel row highlight


    【解决方案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
    

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

    【讨论】:

    • 谢谢安德鲁,对不起我的代码。我无法让它在这里正确显示,即使在编辑模式下它是正确的。我尝试使用您提供的此代码。我仍然有同样的问题。如果您保存、关闭并重新打开它,它仍然会突出显示 2 行。在您的代码中,您实际上需要单击一个单元格一次,然后单击第二个单元格后突出显示开始工作,直到您第一次单击它似乎不会立即工作。谢谢
    • 好的,我更正了帖子中的代码,现在应该可以使用了。我发现我切换了
       命令。我的意思是它现在应该能够复制它,但它在保存、关闭和重新打开时仍然存在问题,导致突出显示两行。
    【解决方案2】:

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

    不管怎样,试试吧:

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

    或者,给 rr 提供 Integer 或 Long 的数据类型,这将假定默认值为 0。

    【讨论】:

    • 顺便说一句,每次移动光标时取消保护和保护工作表似乎是很多不必要的处理。如果它需要不受保护,那么您当然可以提供密码来执行此操作。
    • 另外,您的代码不正确,我假设您的意思是 If rr = "" Then,但如果我对此进行修改,它仍然会产生错误。
    • 嗨,安德鲁,感谢您的帖子,但就其工作方式而言,它似乎没有任何改变。除了我所拥有的之外,我还添加了这一点。
    • 嗯,除此之外,电子表格中还有很多其他代码,并且电子表格由于其他原因受到保护。为了使突出显示正常工作,我首先需要取消保护它。否则,是的,没有必要取消保护和保护它。我只是在一个新的测试电子表格中运行它,其中没有任何其他内容,只是试图在保存并重新打开行时让它工作。一旦它被实施,我将需要在那里有 unprotect,但对于这个测试,不需要它。谢谢。
    • 无论有没有修改,您的代码都对我不起作用。快速解决方法是突出显示整个工作表并使用此公式 =CELL("row")=CELL("row",A1) 创建条件格式规则。这将使用所选颜色突出显示整行,但您需要在单击单元格后按 F9(或键入内容并按 Tab)。该公式可以修改为仅适用于超过 6 的行。或者,有可用的插件。您可以使用一些代码自己实现这一点,但它需要的代码比您当前的代码多一点。
    【解决方案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
    

    【讨论】:

    • 很高兴你把它排序了,很高兴看到更有意义的变量名;)安迪。
    【解决方案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=行() 应用范围为:=$D$8:$R$10000

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 2021-03-19
      相关资源
      最近更新 更多