Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2831|回复: 9

[已解决]请求高手帮忙写2个VBA代码,实现自动添加字体颜色

[复制链接]
发表于 2011-11-19 15:04 | 显示全部楼层 |阅读模式
1.rar (33.71 KB, 下载次数: 16)
发表于 2011-11-19 15:34 | 显示全部楼层
wayy.rar (32.66 KB, 下载次数: 17)
回复

使用道具 举报

 楼主| 发表于 2011-11-19 16:36 | 显示全部楼层
为何我插入一列或几列后修改下面的红色地址后没用呢,谢谢!
Sub AddCor()
Dim i As Long
Dim x As Long
Dim Str As String
Dim t
Dim arr
arr = Range("G1", [L65536].End(xlUp))
For i = 1 To UBound(arr)
  For x = 1 To 6
   Str = arr(i, x)
   Select Case Str
   Case "金"
    Cells(i, x + 6).Font.ColorIndex = 44
   Case "木"
    Cells(i, x + 6).Font.ColorIndex = 50
   Case "水"
    Cells(i, x + 6).Font.ColorIndex = 33
   Case "火"
    Cells(i, x + 6).Font.ColorIndex = 3
   Case "土"
    Cells(i, x + 6).Font.ColorIndex = 16
    End Select
  Next
Next
MsgBox "用时:" & Timer - t
End Sub

Sub DelCor()
Range("G1:L65536").Font.ColorIndex = 0
End Sub

点评

你在哪插入的列?红色的字体表示要修改的区域。  发表于 2011-11-21 08:12
回复

使用道具 举报

 楼主| 发表于 2011-11-20 15:41 | 显示全部楼层
请求高手解决,谢谢~
回复

使用道具 举报

 楼主| 发表于 2011-11-21 18:14 | 显示全部楼层
本帖最后由 夏之恋之夏 于 2011-11-21 21:43 编辑

G列至L列要改为AO列至AL列请问代码怎么修改
1.rar (37.98 KB, 下载次数: 9)
回复

使用道具 举报

 楼主| 发表于 2011-11-23 18:49 | 显示全部楼层
如何修改呢?



回复

使用道具 举报

发表于 2011-11-24 20:50 | 显示全部楼层    本楼为最佳答案   
夏之恋之夏 发表于 2011-11-23 18:49
如何修改呢?
  1. Sub AddCor()
  2. Dim i As Long
  3. Dim x As Long
  4. Dim Str As String
  5. Dim t
  6. Dim arr
  7. arr = Range("AO1", [AT65536].End(xlUp))
  8. For i = 1 To UBound(arr)
  9. For x = 1 To 6
  10.   Str = arr(i, x)
  11.   Select Case Str
  12.   Case "金"
  13.     Cells(i, x + 40).Font.ColorIndex = 44
  14.   Case "木"
  15.     Cells(i, x + 40).Font.ColorIndex = 50
  16.   Case "水"
  17.     Cells(i, x + 40).Font.ColorIndex = 33
  18.   Case "火"
  19.     Cells(i, x + 40).Font.ColorIndex = 3
  20.   Case "土"
  21.     Cells(i, x + 40).Font.ColorIndex = 16
  22.   End Select
  23. Next
  24. Next
  25. MsgBox "用时:" & Timer - t
  26. End Sub
复制代码

回复

使用道具 举报

发表于 2011-11-24 22:03 | 显示全部楼层
本帖最后由 mxg825 于 2011-11-24 22:06 编辑
wayy 发表于 2011-11-24 20:50


这样能否提速一点点。。

  1. Sub AddCor1()
  2. Dim i As Long
  3. Dim x As Long
  4. Dim Str As String
  5. Dim t
  6. Dim arr
  7. Dim 金 As Range, 木 As Range, 水 As Range, 火 As Range, 土 As Range
  8. t = Timer
  9. Application.ScreenUpdating = False
  10. arr = Range("AO1", [AT65536].End(xlUp))
  11. Set 金 = Range("AV2")
  12. Set 木 = Range("AW2")
  13. Set 水 = Range("AX2")
  14. Set 火 = Range("AY2")
  15. Set 土 = Range("AZ2")
  16. For i = 1 To UBound(arr)
  17. For x = 1 To 6
  18.   Str = arr(i, x)
  19.   Select Case Str
  20.   Case "金"
  21.         Set 金 = Union(金, Cells(i, x + 40))
  22.   Case "木"
  23.         Set 木 = Union(木, Cells(i, x + 40))
  24.   Case "水"
  25.         Set 水 = Union(水, Cells(i, x + 40))
  26.   Case "火"
  27.         Set 火 = Union(火, Cells(i, x + 40))
  28.   Case "土"
  29.         Set 土 = Union(土, Cells(i, x + 40))
  30.   End Select
  31. Next
  32. Next
  33. 金.Font.ColorIndex = 44
  34. 木.Font.ColorIndex = 50
  35. 水.Font.ColorIndex = 33
  36. 火.Font.ColorIndex = 3
  37. 土.Font.ColorIndex = 16
  38. Application.ScreenUpdating = True
  39. MsgBox "用时:" & Timer - t
  40. End Sub
复制代码

点评

合并后统一设置格式,应该可以提速。  发表于 2011-11-24 22:17
回复

使用道具 举报

 楼主| 发表于 2011-11-24 22:15 | 显示全部楼层
没发现提速,反而代码好像有误,陷入死循环中,EXCEL没有响应了!
回复

使用道具 举报

发表于 2011-11-24 22:40 | 显示全部楼层
夏之恋之夏 发表于 2011-11-24 22:15
没发现提速,反而代码好像有误,陷入死循环中,EXCEL没有响应了!

不会死循环呀!
我只是把屏幕刷新 暂时关闭!和最后统一设置!

有提速10% 左右!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-17 00:49 , Processed in 0.333397 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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