Excel精英培训网

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

[已解决]请教颜色代码如何实现

[复制链接]
发表于 2014-3-24 13:31 | 显示全部楼层 |阅读模式
本帖最后由 qh8600 于 2014-3-24 15:26 编辑

颜色.rar (7.47 KB, 下载次数: 5)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-3-24 13:55 | 显示全部楼层
底纹色是修改range对象的interior来实现。
回复

使用道具 举报

发表于 2014-3-24 13:58 | 显示全部楼层
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     If Target.CountLarge > 1 Then Exit Sub
  3.     If Target.Column <> 6 Then Exit Sub
  4.     If Len(Target.Value) = 0 Then
  5.         Target.Interior.ColorIndex = 0
  6.     End If
  7.     Dim rg As Range
  8.     Set rg = Columns(1).Find(what:=Target.Value, lookat:=xlWhole)
  9.     If rg Is Nothing Then
  10.         Target.Interior.ColorIndex = 0
  11.     Else
  12.         Target.Interior.ColorIndex = rg.Offset(, 1).Value
  13.     End If
  14. End Sub
复制代码
回复

使用道具 举报

发表于 2014-3-24 13:59 | 显示全部楼层
颜色.rar (10.05 KB, 下载次数: 4)
回复

使用道具 举报

发表于 2014-3-24 14:00 | 显示全部楼层    本楼为最佳答案   
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     With Target
  3.         If .CountLarge > 1 Then Exit Sub
  4.         If .Column <> 6 Then Exit Sub
  5.         If Len(.Value) = 0 Then
  6.             .Interior.ColorIndex = 0
  7.             Exit Sub
  8.         End If

  9.         Dim rg As Range
  10.         Set rg = Columns(1).Find(what:=.Value, lookat:=xlWhole)
  11.         If rg Is Nothing Then
  12.             .Interior.ColorIndex = 0
  13.         Else
  14.             .Interior.ColorIndex = rg.Offset(, 1).Value
  15.         End If
  16.     End With
  17. End Sub
复制代码
上面有点小BUG,忘了加EXIT SUB了。
回复

使用道具 举报

发表于 2014-3-24 14:06 | 显示全部楼层
另外,B列颜色码没有做校验,假设你输入的颜色码都是合法的,否则会要报错。
当B列颜色列为无效颜色值,则背景填充色直接改为无色
  1.     On Error Resume Next
  2.     With Target
  3.         If .CountLarge > 1 Then Exit Sub
  4.         If .Column <> 6 Then Exit Sub
  5.         If Len(.Value) = 0 Then
  6.             .Interior.ColorIndex = xlColorIndexNone
  7.             Exit Sub
  8.         End If

  9.         Dim rg As Range
  10.         Set rg = Columns(1).Find(what:=.Value, lookat:=xlWhole)
  11.         If rg Is Nothing Then
  12.             .Interior.ColorIndex = xlColorIndexNone
  13.         Else
  14.             .Interior.ColorIndex = rg.Offset(, 1).Value
  15.             If Err.Number <> 0 Then
  16.                 .Interior.ColorIndex = xlColorIndexNone
  17.             End If
  18.         End If
  19.     End With
  20. End Sub
复制代码
回复

使用道具 举报

发表于 2014-3-24 14:07 | 显示全部楼层
  1. Sub Macro1()
  2.     Dim arr, k%, i%
  3.     arr = Range("a1").CurrentRegion
  4.     For k = 2 To Cells(Rows.Count, 6).End(3).Row
  5.         For i = 2 To UBound(arr)
  6.             If Cells(k, 6) = arr(i, 1) Then
  7.                 Cells(k, 6).Interior.ColorIndex = Cells(i, 3).Interior.ColorIndex
  8.             End If
  9.         Next i
  10.     Next k
  11. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-3-24 15:26 | 显示全部楼层
风林火山 发表于 2014-3-24 14:07

谢谢老师,你写的同样可以实现。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 03:05 , Processed in 0.360432 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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