Excel精英培训网

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

[已解决]求高手,用VBA怎么实现这样的功能?相信一定会有好心的高手帮忙。

[复制链接]
发表于 2012-11-8 18:29 | 显示全部楼层 |阅读模式
5学分
本帖最后由 excelers 于 2012-11-8 18:40 编辑

单元格里面的数字是有由若干(个数不确定,变动的)个01到11的数字组成,怎么样能统计01到11这11个数字在单元格里面各出现的次数(可能是0次),然后将出现次数第最多,第二多,最少,第二少的数字显示出来。并判断得出的号码里面有没有包含特定的那个数字,如果包含了,将该单元格的底纹设置为红色。
最佳答案
2012-11-8 20:51
  1. Sub FindNo()
  2. Cells.Interior.ColorIndex = 0
  3. Dim d, i%, j%, T%, arr
  4. Set d = CreateObject("Scripting.Dictionary")
  5. For i = 1 To 11
  6.    T = (Len(Cells(3, 6)) - Len(Replace(Cells(3, 6), Format(i, "00"), ""))) \ 2
  7.    If d.exists(T) Then
  8.         d(T) = d(T) & "," & Format(i, "00")
  9.    Else
  10.         d.Add T, Format(i, "00")
  11.    End If
  12. Next i
  13. Cells(3, 7) = Application.WorksheetFunction.Large(d.keys, 1)
  14. Cells(3, 8) = d(Cells(3, 7).Value)
  15. If InStr(Cells(3, 8), Cells(3, 3)) Then Cells(3, 8).Interior.ColorIndex = 3
  16. Cells(3, 9) = Application.WorksheetFunction.Large(d.keys, 2)
  17. Cells(3, 10) = d(Cells(3, 9).Value)
  18. If InStr(Cells(3, 10), Cells(3, 3)) Then Cells(3, 10).Interior.ColorIndex = 3
  19. End Sub
复制代码
提供一个思路……剩下的自己组织吧

Book1.zip

2.81 KB, 下载次数: 10

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-11-8 18:36 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2012-11-8 18:40 | 显示全部楼层
兰色幻想 发表于 2012-11-8 18:36
做一个样表传上来吧

记得传了一个附件的。

Book1.zip

2.81 KB, 下载次数: 6

回复

使用道具 举报

发表于 2012-11-8 20:51 | 显示全部楼层    本楼为最佳答案   
  1. Sub FindNo()
  2. Cells.Interior.ColorIndex = 0
  3. Dim d, i%, j%, T%, arr
  4. Set d = CreateObject("Scripting.Dictionary")
  5. For i = 1 To 11
  6.    T = (Len(Cells(3, 6)) - Len(Replace(Cells(3, 6), Format(i, "00"), ""))) \ 2
  7.    If d.exists(T) Then
  8.         d(T) = d(T) & "," & Format(i, "00")
  9.    Else
  10.         d.Add T, Format(i, "00")
  11.    End If
  12. Next i
  13. Cells(3, 7) = Application.WorksheetFunction.Large(d.keys, 1)
  14. Cells(3, 8) = d(Cells(3, 7).Value)
  15. If InStr(Cells(3, 8), Cells(3, 3)) Then Cells(3, 8).Interior.ColorIndex = 3
  16. Cells(3, 9) = Application.WorksheetFunction.Large(d.keys, 2)
  17. Cells(3, 10) = d(Cells(3, 9).Value)
  18. If InStr(Cells(3, 10), Cells(3, 3)) Then Cells(3, 10).Interior.ColorIndex = 3
  19. End Sub
复制代码
提供一个思路……剩下的自己组织吧
回复

使用道具 举报

 楼主| 发表于 2012-11-8 21:15 | 显示全部楼层
suye1010 发表于 2012-11-8 20:51
提供一个思路……剩下的自己组织吧

我蒙了一下large的反义词small,好像是可以。

谢谢。
回复

使用道具 举报

 楼主| 发表于 2012-11-8 21:40 | 显示全部楼层
suye1010 发表于 2012-11-8 20:51
提供一个思路……剩下的自己组织吧


当数据为空的时候,求大值会报错。可以怎么修改吗?
回复

使用道具 举报

 楼主| 发表于 2012-11-9 10:12 | 显示全部楼层
suye1010 发表于 2012-11-8 20:51
提供一个思路……剩下的自己组织吧

非常谢谢。在您的引导下,我瞎蒙出了需要的效果。


Cells.Interior.ColorIndex = 0
Dim d, b, i%, j%, T%, arr
Set d = CreateObject("Scripting.Dictionary")
Set b = CreateObject("Scripting.Dictionary")
For i = 1 To 11
   T = (Len(Cells(3, 6)) - Len(Replace(Cells(3, 6), Format(i, "00"), ""))) \ 2
   If d.exists(T) Then
        d(T) = d(T) & "," & Format(i, "00")
   Else
        d.Add T, Format(i, "00")
   End If
Next i
If Cells(3, 6).Value <> "" Then
Cells(3, 7) = Application.WorksheetFunction.Large(d.keys, 1)
Cells(3, 8) = d(Cells(3, 7).Value)
If InStr(Cells(3, 8), Cells(3, 3)) Then Cells(3, 8).Interior.ColorIndex = 3
Cells(3, 9) = Application.WorksheetFunction.Large(d.keys, 2)
Cells(3, 10) = d(Cells(3, 9).Value)
If InStr(Cells(3, 10), Cells(3, 3)) Then Cells(3, 10).Interior.ColorIndex = 3
Else
Cells(3, 7).Value = "空"
Cells(3, 8).Value = "空"
Cells(3, 9).Value = "空"
Cells(3, 10).Value = "空"
End If

For i = 1 To 11
   T = (Len(Cells(6, 6)) - Len(Replace(Cells(6, 6), Format(i, "00"), ""))) \ 2
   If b.exists(T) Then
        b(T) = b(T) & "," & Format(i, "00")
   Else
        b.Add T, Format(i, "00")
   End If
Next i
If Cells(6, 6).Value <> "" Then
Cells(6, 7) = Application.WorksheetFunction.Small(b.keys, 1)
Cells(6, 8) = b(Cells(6, 7).Value)
If InStr(Cells(6, 8), Cells(3, 3)) Then Cells(6, 8).Interior.ColorIndex = 3
Cells(6, 9) = Application.WorksheetFunction.Small(b.keys, 2)
Cells(6, 10) = b(Cells(6, 9).Value)
If InStr(Cells(6, 10), Cells(3, 3)) Then Cells(6, 10).Interior.ColorIndex = 3
Else
Cells(6, 7).Value = "空"
Cells(6, 8).Value = "空"
Cells(6, 9).Value = "空"
Cells(6, 10).Value = "空"
End If

评分

参与人数 1 +1 金币 +1 收起 理由
suye1010 + 1 + 1 赞扬一下你的学习精神!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-13 11:43 , Processed in 0.373156 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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