【问题标题】:How to highlight cells with decimals or characters less than 3 and more than 6 using vba?如何使用 vba 突出显示小数或字符小于 3 且大于 6 的单元格?
【发布时间】:2016-12-07 16:21:01
【问题描述】:

我是 VBA 编码的新手,请帮助我创建具有以下条件的 VBA 脚本。

  1. 应突出显示包含小数的单元格。
  2. 应突出显示字符数少于 3 或多于 6 的单元格。
  3. 应该从 G 列 (G1) 执行到最后一行最后使用的单元格。

我的数据是字母数字或数字。

我尝试过使用characters.countValue.count,但没有成功。希望它适用于len,但我不知道如何开始。

附件是带有突出显示单元格的示例 excel 文件

我已经尝试了下面的代码。因为我的数据是字母数字的,所以字符数没有帮助。

Sub HighlightCells()
Range(" G1").Select
Do
  If ActiveCell.Characters.Count < 3 Then
    With Selection.Interior
      .Pattern = xlSolid
      .PatternColorIndex = xlAutomatic
      .Color = 65535
      .TintAndShade = 0
      .PatternTintAndShade = 0
    End With
  End If
  ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row     last used cell
Loop Until ActiveCell = ""

Range(" G1").Select
Do
  If ActiveCell.Characters.Count > 6 Then
    With Selection.Interior
      .Pattern = xlSolid
      .PatternColorIndex = xlAutomatic
      .Color = 65535
      .TintAndShade = 0
      .PatternTintAndShade = 0
    End With
  End If
  ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row   last used cell
Loop Until ActiveCell = ""
End Sub

【问题讨论】:

  • 条件是AND条件还是OR条件???
  • 如果单元格包含 no 个字符,是否要突出显示它???
  • 条件只有 OR。没有字符的空白单元格不需要突出显示。

标签: excel vba


【解决方案1】:

之前:

这段代码几乎是将你的英文描述直接翻译成VBA:

Sub Dural()
    Dim N As Long, i As Long, s As String, L As Long

    N = Cells(Rows.Count, "G").End(xlUp).Row
    For i = 1 To N
        s = Cells(i, "G").Text
        L = Len(s)
        If InStr(1, s, ".") > 0 Or (L < 3 Or L > 6) Then
            With Cells(i, "G").Interior
              .Pattern = xlSolid
              .PatternColorIndex = xlAutomatic
              .Color = 65535
              .TintAndShade = 0
              .PatternTintAndShade = 0
            End With
        End If
    Next i
End Sub

之后:

【讨论】:

  • 谢谢加里。它工作得很好..!!您是否也可以在其中包含其他行。我的意思是 G,H,I,J...BX,BY,BZ。它现在只在 G 行有效。
  • @Mahesh 我需要一份完整的清单
  • 嗨,Gary,我无法完全看到您的评论。
【解决方案2】:
Sub Test()
    Application.ScreenUpdating = False
    LastRow = Rows(ActiveSheet.UsedRange.Row + _
    ActiveSheet.UsedRange.Rows.Count - 1).Row
    LastCol = Columns(ActiveSheet.UsedRange.Column + _
    ActiveSheet.UsedRange.Columns.Count - 1).Column
    For Each cll In Range(Cells(1, 7), Cells(LastRow, LastCol))
    s = cll.Value
    l = Len(s)
    If ((l > 0) And (l < 3)) Or (l > 6) Or (s Like "*#.#*") _
        Then cll.Interior.Color = vbRed
    Next cll
    Application.ScreenUpdating = True

结束子

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    相关资源
    最近更新 更多