【问题标题】:Highlighting cell values based on column text基于列文本突出显示单元格值
【发布时间】:2018-05-31 01:58:05
【问题描述】:

我有一个 3000 行和 F 列到 BQ 的 Excel 表。我想突出显示值 > 0 的特定单元格,其中 F 列中的文本包含 "X" 。如何为它编写 VBA 宏?

我尝试了以下代码,但没有成功:

Sub ConCol()
Set iRow = Range("F2:BQ3000")

For Each cell In iRow
  If cell.Value <> "" And Columns("$F") = "X" Then
cell.Interior.Color = RGB(255, 105, 108)
End If
Next
End Sub

【问题讨论】:

  • 您可以使用条件格式来做到这一点——您绝对需要使用 VBA 吗?
  • 如果您必须使用 VBA,请注意 cells that has value &gt; 0 不等同于 cell.Value &lt;&gt; "",您应该改用 cell.Value &gt; 0。但正如 BigBen 所提到的,条件格式完全符合您的要求。
  • 是的!所以我可以让这个过程可重复,并且可以为其他文本实现
  • @Goal7 然后,不要将公式中的文本硬编码为条件格式,而是引用一个单元格。更改单元格的值,突出显示不同的单元格
  • 您的意思是让单元格“包含 X”还是等于“X”?

标签: vba excel


【解决方案1】:

试试这个代码,它在我这边运行良好。 如果 F 列包含 X,我会突出显示一行中包含 X 的所有单元格。

Sub ConCol()

'loop on each row
For i = 1 To 3000 ' row

    If InStr(1, ActiveSheet.Cells(i, 6).Value, "X") > 0 Then ' first condition : if col F contain X
        'loop on each column BQ = 69
        For j = 1 To 69

        If InStr(1, ActiveSheet.Cells(i, j).Value, "x") > 0 Then 'second condition : in each cell a selected row, if contain X
        ActiveSheet.Cells(i, j).Interior.Color = RGB(255, 105, 108)
        End If

        Next j

    End If

Next i

End Sub

或者这个版本,我只高亮F列:

Sub ConCol2()

'loop on each row
For i = 1 To 3000 ' row

    If InStr(1, ActiveSheet.Cells(i, 6).Value, "X") > 0 Then

        ActiveSheet.Cells(i, 6).Interior.Color = RGB(255, 105, 108)

    End If

Next i

End Sub

【讨论】:

    【解决方案2】:

    会有几种变化:

    Sub Button2_Click()
        Dim LstRw As Long, Rng As Range, Sh As Worksheet, c As Range
    
        Set Sh = ActiveSheet
        With Sh
            LstRw = .Cells(.Rows.Count, "F").End(xlUp).Row
            Set Rng = .Range("G2:BQ" & LstRw)
            For Each c In Rng.SpecialCells(xlCellTypeConstants, 1)
                If c > 0 Then
                    If InStr(UCase(.Cells(c.Row, "F")), "X") Then
                        c.Interior.Color = RGB(255, 105, 108)
                    End If
                End If
            Next c
        End With
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      这不是优化的,而是在范围内的行的 F 列中查找大写的“X”。如果存在,它会继续查看其他列中的每个单元格,如果 Isnumeric 和 > 0 它会被着色。

      Option Explicit
      
      Public Sub ConCol()
          Application.ScreenUpdating = False
          Dim currentRow As Range, loopRange As Range, currentCell As Range
      
          With ActiveSheet
      
              Set loopRange = .Range("F2:BQ3000")
      
              For Each currentRow In loopRange.Rows
                  If InStr(1, currentRow.Columns(1).Value, "X", vbBinaryCompare) > 0 Then
                      For Each currentCell In currentRow.Cells
                          If IsNumeric(currentCell.Value) And currentCell > 0 Then
                              currentCell.Interior.Color = RGB(255, 105, 108)
                          End If
                      Next currentCell
                  End If
              Next
      
          End With
      
          Application.ScreenUpdating = True
      
      End Sub
      

      【讨论】:

      • 请注意,我的解决方案明确检查单元格中的实际大写 X,而不仅仅是任何 x。所以要确定你在追求哪个。任何 x 或 X。在测试之前简单地转换为大写是不一样的。取决于你追求什么。
      猜你喜欢
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多