Excel精英培训网

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

[已解决]按要求填充颜色

[复制链接]
发表于 2016-6-9 10:48 | 显示全部楼层 |阅读模式
按要求填充颜色
最佳答案
2016-6-9 13:35
代码如下:
  1. Sub xx()
  2.     Dim arr(), brr(), r&, c&, j&, i&, rng As Range, d1 As Object, d2 As Object
  3.     Set rng = Sheet1.Range("B8:T12") '自己设定范围
  4.     arr = Array(0, 1, 4, 5, 8)
  5.     brr = Array(2, 3, 6, 7, 9)
  6.     Set d1 = CreateObject("Scripting.Dictionary")
  7.     Set d2 = CreateObject("Scripting.Dictionary")
  8.     For i = 0 To UBound(arr)
  9.         d1.Add arr(i), ""
  10.         d2.Add brr(i), ""
  11.     Next
  12.     With rng
  13.         r = rng.Rows.Count
  14.         c = rng.Columns.Count
  15.         For i = 1 To c
  16.             For j = 2 To r
  17.                 If d1.Exists(rng(j - 1, i).Value) And d2.Exists(rng(j, i).Value) Then rng(j, i).Interior.Color = 192
  18.                 If d2.Exists(rng(j - 1, i).Value) And d1.Exists(rng(j, i).Value) Then rng(j, i).Interior.Color = 192
  19.             Next
  20.         Next
  21.     End With
  22. End Sub
复制代码

代码2.zip

8.35 KB, 下载次数: 15

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2016-6-9 13:35 | 显示全部楼层    本楼为最佳答案   
代码如下:
  1. Sub xx()
  2.     Dim arr(), brr(), r&, c&, j&, i&, rng As Range, d1 As Object, d2 As Object
  3.     Set rng = Sheet1.Range("B8:T12") '自己设定范围
  4.     arr = Array(0, 1, 4, 5, 8)
  5.     brr = Array(2, 3, 6, 7, 9)
  6.     Set d1 = CreateObject("Scripting.Dictionary")
  7.     Set d2 = CreateObject("Scripting.Dictionary")
  8.     For i = 0 To UBound(arr)
  9.         d1.Add arr(i), ""
  10.         d2.Add brr(i), ""
  11.     Next
  12.     With rng
  13.         r = rng.Rows.Count
  14.         c = rng.Columns.Count
  15.         For i = 1 To c
  16.             For j = 2 To r
  17.                 If d1.Exists(rng(j - 1, i).Value) And d2.Exists(rng(j, i).Value) Then rng(j, i).Interior.Color = 192
  18.                 If d2.Exists(rng(j - 1, i).Value) And d1.Exists(rng(j, i).Value) Then rng(j, i).Interior.Color = 192
  19.             Next
  20.         Next
  21.     End With
  22. End Sub
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 16:37 , Processed in 0.479838 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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