Excel精英培训网

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

[已解决]求助论坛前辈,vba批量填充字体颜色

[复制链接]
发表于 2012-7-23 08:43 | 显示全部楼层 |阅读模式
本帖最后由 yslvictor 于 2012-7-23 08:52 编辑

我是vba入门,实际运用中需要把下列表按要求把D:M列填充字体颜色,请老师帮忙写段代码。

图1为填充i前
图2填充后








最佳答案
2012-7-23 10:15
  1. Sub aa()
  2. Dim x As Integer, y As Integer, i As Integer
  3. For i = 2 To Range("b65536").End(3).Row
  4. For x = 1 To 100
  5. For y = 4 To 13
  6.     If Cells(x, y) = Cells(i, 2) Then
  7.         Cells(x, y).Font.ColorIndex = Cells(i, 1)
  8.     End If
  9. Next y
  10. Next x
  11. Next i
  12. End Sub
复制代码
1.jpg
2.jpg

填充颜色.rar

4.88 KB, 下载次数: 19

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-7-23 09:37 | 显示全部楼层
  1. Sub a()
  2. Dim rng As Range
  3. Dim x%
  4. For Each rng In Range("d:m")
  5. For x = 2 To Range("b65536").End(3).Row
  6. If rng = Cells(x, 2) Then
  7. rng.Font.ColorIndex = Cells(x, 1)
  8. End If
  9. Next x
  10. Next rng
  11. End Sub
复制代码
回复

使用道具 举报

发表于 2012-7-23 09:42 | 显示全部楼层
本帖最后由 桀骜孤星 于 2012-7-23 09:45 编辑

  1. Sub test()
  2. Dim rng As Range

  3. Set d = CreateObject("scripting.dictionary")
  4. For i = 2 To [a65536].End(3).Row
  5. d(Cells(i, 2).Value) = Cells(i, 1).Value
  6. Next

  7. For Each rng In Sheets("要填充字体").Range("d1:m1000")
  8. If rng <> "" Then

  9. rng.Font.ColorIndex = d(rng.Value)
  10. End If
  11. Next

  12. End Sub
复制代码
填充颜色.rar (9.82 KB, 下载次数: 10)
回复

使用道具 举报

发表于 2012-7-23 10:15 | 显示全部楼层    本楼为最佳答案   
  1. Sub aa()
  2. Dim x As Integer, y As Integer, i As Integer
  3. For i = 2 To Range("b65536").End(3).Row
  4. For x = 1 To 100
  5. For y = 4 To 13
  6.     If Cells(x, y) = Cells(i, 2) Then
  7.         Cells(x, y).Font.ColorIndex = Cells(i, 1)
  8.     End If
  9. Next y
  10. Next x
  11. Next i
  12. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2012-7-23 14:51 | 显示全部楼层
谢谢论坛前辈,你们辛苦了。
回复

使用道具 举报

发表于 2012-7-27 11:51 | 显示全部楼层
最佳答案果然写得运算又快又好
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-17 10:06 , Processed in 0.975298 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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