A macro to get all interior colorindex has been used in thisworkbook

本文提供两种使用VBA获取Excel工作簿中所有已使用的颜色索引的方法:一种是通过集合,另一种是通过字典。这两种方法均遍历每张工作表的已使用范围,收集颜色索引,并最终显示所有被使用过的颜色索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1集合的方法:

Sub getallcolor()
Dim sh As Worksheet, x As New Collection, colors(), c As Range, i As Long
On Error Resume Next
For Each sh In Sheets
For Each c In sh.UsedRange
x.Add c.Interior.ColorIndex, "key" & c.Interior.ColorIndex
Next
Next
ReDim colors(1 To x.Count)
For i = 1 To x.Count
colors(i) = x(i)
Next
MsgBox "The following colorindex has been used in thisworkbook:" & vbCrLf & vbCrLf & Join(colors, vbCrLf)
End Sub

2字典的方法:

Sub getallcolor()
Dim sh As Worksheet, r As Range, mycolor As Integer
With CreateObject("scripting.dictionary")
For Each sh In Sheets
For Each r In sh.UsedRange
mycolor = r.Interior.ColorIndex
If Not .exists(mycolor) Then .Add mycolor, Nothing
Next
Next
MsgBox "The following colorindex has been used in thisworkbook:" & vbCrLf & Join(.keys, vbLf)
End With
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值