VBA,群里求助的问题,代码没看懂,和数据有关系,和改进代码

原始问题

  • 我测试了下,确实显示越界
  • 但和表上数据还有关系,改表数据,会出现其他地方越界
  •  

Sub t3()
Dim arr, t
Dim x, x1 As Integer
Dim str, str1 As String
   清空颜色
   t = Timer
   arr = Range("d2:d" & Range("d65536").End(xlUp).Row)
   For x = 1 To UBound(arr) + 1
   If x = UBound(arr) + 1 Then Application.Intersect(Range("a:d"), Range(Left(str, Len(str) - 1))).Interior.ColorIndex = 3
     If arr(x, 1) >= 300 Then
        str1 = str 'str1作用是啥[str1恢复用的容器]
        x1 = x + 1
        Do
        x = x + 1
        Loop Until arr(x, 1) < 300
        str = str & x1 & ":" & x & ","
        If Len(str) > 255 Then
        str = str1
        x = x1 - 1 '作用是啥
        Application.Intersect(Range("a:d"), Range(Left(str, Len(str) - 1))).Interior.ColorIndex = 3
        str = ""
        End If
      x = x - 1 '作用是啥
      End If
    Next x
 MsgBox Timer - t '为什么减t
End Sub

2 这段代码写的很飘逸,看不懂,很多疑点

   If x = UBound(arr) + 1 Then Application.Intersect(Range("a:d"), Range(Left(str, Len(str) - 1))).Interior.ColorIndex = 3

str1 = str 'str1作用是啥[str1恢复用的容器]
        x1 = x + 1
        Do
        x = x + 1
        Loop Until arr(x, 1) < 300
        str = str & x1 & ":" & x & ","
        If Len(str) > 255 Then

别人的改进代码,效果几乎一样,写的很清晰

Sub 颜色填充()
    Dim i%, s$, rng As Range, rnk As Range
    For Each rng In Range("d2:d" & Cells(Rows.Count, 4).End(xlUp).Row)
        If rng.Value >= 300 Then
            Range(rng.Offset(0, -3), rng).Interior.ColorIndex = 3
        End If
    Next
End Sub

发布了416 篇原创文章 · 获赞 46 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/xuemanqianshan/article/details/104262587