gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:52:44 25 4
gpt4 key购买 nike

我正在尝试编写一个宏来检查 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com