Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
楼主: 兰色幻想

VBA80集第24集答疑专贴

  [复制链接]
发表于 2013-5-28 10:41 | 显示全部楼层
Sub 数组方法2()
t = Timer
arr = Range("d2:d" & Range("a65536").End(xlUp).Row)
For x = 1 To UBound(arr)       '这里的arr数组下标从1开始?不是默认从0开始的?
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2013-6-22 02:30 | 显示全部楼层
x = x-1 应该放到上一个if判断的里面 我认为
回复

使用道具 举报

发表于 2013-9-3 16:33 | 显示全部楼层
Sub 数组方法4()
Dim arr, t
Dim I%
Dim str$, str1$
清除颜色
t = Timer
arr = Range("D2:D" & Range("A65536").End(3).Row)
For I = 1 To UBound(arr)
If arr(I, 1) > 500 Then
str1 = str
str = str & I + 1 & ":" & I + 1 & ","
If Len(str) > 255 Then
str = str1
Application.Intersect(Range("a:d"), Range(Left(str, Len(str) - 1))).Interior.ColorIndex = 3
str = ""
I = I - 1
End If
End If
Next
If I - 1 = UBound(arr) Then Application.Intersect(Range("a:d"), Range(Left(str, Len(str) - 1))).Interior.ColorIndex = 3
[i11] = Timer - t
End Sub
回复

使用道具 举报

发表于 2013-9-3 16:34 | 显示全部楼层
Sub 数组方法5()
Dim arr, t
Dim I%
Dim str$, str1$
清除颜色
t = Timer
arr = Range("d2:d" & Range("a65536").End(3).Row)
For I = 1 To UBound(arr)
    If arr(I, 1) > 500 Then
    str1 = str
    str = str & "A" & I + 1 & ":D" & I + 1 & ","
        If Len(str) > 255 Then
        str = str1
        Range(Left(str, Len(str) - 1)).Interior.ColorIndex = 3
        str = ""
        I = I - 1
        End If
End If
Next
If I - 1 = UBound(arr) Then Range(Left(str, Len(str) - 1)).Interior.ColorIndex = 3
[i13] = Timer - t
End Sub
回复

使用道具 举报

发表于 2013-9-3 16:34 | 显示全部楼层
Sub 数组方法6()
Dim arr, t
Dim I%, J%
Dim str$, str1$
清除颜色
t = Timer
arr = Range("D2:D" & Range("A65536").End(3).Row)
For I = 1 To UBound(arr)
    If arr(I, 1) > 500 Then
    str1 = str
     I = I + 1
     J = I - 1
    Do
    J = J + 1
    Loop Until arr(J, 1) <= 500
    str = str & I & ":" & J & ","
        If Len(str) > 255 Then
        str = str1
        Application.Intersect(Range("a:d"), Range(Left(str, Len(str) - 1))).Interior.ColorIndex = 3
        str = ""
        I = I - 2
        End If
    End If
Next
If I - 1 = UBound(arr) Then Application.Intersect(Range("a:d"), Range(Left(str, Len(str) - 1))).Interior.ColorIndex = 3
[i15] = Timer - t
End Sub
回复

使用道具 举报

发表于 2013-10-31 23:42 | 显示全部楼层
本帖最后由 mima1996 于 2013-10-31 23:43 编辑

arr = Range("d2:d" & Range("a65536").End(xlUp).Row)
另一种写法:
dim len as long
len = activesheet.cells.specialcells(xllastcell).row
arr = range("D2:d" & len)
请教兰老师第二种写法是否更可靠,因为可以避免行数超过65536行的问题?是否有其它的原因导致要使用原方法?
回复

使用道具 举报

发表于 2013-11-2 18:32 | 显示全部楼层
奇怪,怎么没有人答疑?哪位高手帮忙解答一下。主要是两种用法有什么区别?
Range("a65536").End(xlUp)
Cells.SpecialCells(xlLastCell)
回复

使用道具 举报

发表于 2013-11-10 21:47 | 显示全部楼层
本帖最后由 xjsquall 于 2013-11-10 21:51 编辑
llh505 发表于 2013-5-28 10:28
我把最后一行改成大于500的数,运行数组方法一,最后一行没有填充;数组方法二显示下标越界,这是怎么回事?


我也发现了这个问题,个人认为应该这么改一下
  1. Sub 数组方法3()
  2. Dim arr, t
  3. Dim x As Integer, x1 As Integer
  4. Dim sr As String, sr1 As String
  5. 清除颜色
  6. t = Timer
  7. arr = Range("d2:d" & Range("a65536").End(xlUp).Row)
  8. For x = 1 To UBound(arr)
  9.    If arr(x, 1) > 500 Then
  10.       sr1 = sr
  11.       x1 = x + 1
  12.       On Error Resume Next
  13.       Do
  14.         x = x + 1
  15.       Loop Until arr(x, 1) <= 500
  16.       On Error GoTo 0
  17.       
  18.       sr = sr & x1 & ":" & x & ","
  19.       If Len(sr) > 255 Then
  20.         sr = sr1
  21.         x = x1 - 1
  22.         Application.Intersect(Range("a:d"), Range(Left(sr, Len(sr) - 1))).Interior.ColorIndex = 3
  23.         sr = ""
  24.       End If
  25.       x = x - 1
  26.    End If
  27.    If x = UBound(arr) Then Application.Intersect(Range("a:d"), Range(Left(sr, Len(sr) - 1))).Interior.ColorIndex = 3
  28. Next x
  29. MsgBox Timer - t
  30. End Sub
复制代码
回复

使用道具 举报

发表于 2013-11-19 23:30 | 显示全部楼层
好难,好难。。。。不过我相信对于我做PMC需要的应该可以足够了
回复

使用道具 举报

发表于 2014-3-31 13:22 | 显示全部楼层
For x = 1 To UBound(arr)
   If x = UBound(arr) And sr <> "" Then Range(Left(sr, Len(sr) - 1)).Interior.ColorIndex = 3
   If arr(x, 1) > 500 Then
      sr1 = sr
      sr = sr & "A" & x + 1 & ":D" & x + 1 & ","
      If Len(sr) > 255 Then
        sr = sr1
        Range(Left(sr, Len(sr) - 1)).Interior.ColorIndex = 3
        sr = ""
      End If
   End If
Next x
在这段代码中      
1.  If x = UBound(arr) And sr <> "" Then Range(Left(sr, Len(sr) - 1)).Interior.ColorIndex = 3
没有判断最后一个数据你就直接填色是出错了?  
2.  当我循环到假如200次时   sr = sr & "A" & x + 1 & ":D" & x + 1 & ","  这个连起来的字符正好大于255
这时就会用上次的 sr  进行填色(第200次符合的那个单元格就没用到)。之后你设置sr = "" ,然后next循环到201次 ,那第200次符合的那个单元格就没计入到sr了  不就遗漏了?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 10:33 , Processed in 0.311809 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表