excel - VBA如果单元格小于一定长度,突出显示并显示消息

标签 excel vba highlight

我正在尝试编写一个宏来检查 Excel 电子表格中的某个列,以查找小于 9 个字符但大于 2 个字符的条目,如果找到,则显示一条消息并突出显示找到该值的单元格。它可能会发生多次。我写了以下代码:

Sub Highlight()
Dim c As Range
Dim LR As Integer
Dim intCell As Long
LR = Worksheets("Basket").Cells(Rows.Count, 6).End(xlUp).Row
For intCell = 1 To 8
For Each c In Range("G20:G" & LR).Cells
    If Len(c.Value) < 9 And Len(c.Value) > 2 Then
    MsgBox "One or more of the codes is invalid.  Correct the highlighted values."
    c.Cells(intCell).Interior.Color = vbYellow
    End If
Next
Next
End Sub

我无法弄清楚我做错了什么。任何帮助将不胜感激。

最佳答案

只是猜测你想强调什么

Sub Highlight()
Dim c As Range
Dim LR As Integer
Dim numProbs as long
Dim sht as Worksheet

Set sht=Worksheets("Basket")

numProbs=0
LR = sht.Cells(Rows.Count, "G").End(xlUp).Row
For Each c In sht.Range("G20:G" & LR).Cells
    If Len(c.Value) < 9 And Len(c.Value) > 2 Then
        c.entirerow.cells(1).Resize(1,8).Interior.Color = vbYellow
        numProbs=numProbs+1
    End If
Next

if numProbs>0 Then
    msgbox "There were issues with " & numProbs & " rows. See yellow cells"
end if 


End Sub

关于excel - VBA如果单元格小于一定长度,突出显示并显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18220997/

相关文章:

excel - 在 Excel 中计算多数值

python - 未合并的单元格 For 循环跳过单元格

ms-access - 在 Access 时间格式中自定义文本框

Excel VBA 文本框时间验证为 [h] :mm

php - 突出显示 Div 标签下的文本

vba - 在 Excel 文件夹内的所有文件上添加一列

excel - 卡住。我需要一些 VBA 代码来在 Excel 范围内的每个单元格中的字符串周围插入双引号

c# - VS 2017 文档级加载项构建因 com 可见组件而失败

javascript - jquery 如何知道所选/突出显示文本的顺序出现

d3.js - 如何突出显示多个圆环图 d3 的切片