Excel精英培训网

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

[已解决]VBA求助

[复制链接]
发表于 2022-6-6 13:02 | 显示全部楼层 |阅读模式
大家好,我想从表格中把不同填充颜色的单元格数据导出成一列数据,怎么编写?请详细解释一下,谢谢!
最佳答案
2022-6-6 15:34
Sub SplitColor()
    Dim Rng As Range
    With CreateObject("scripting.dictionary")
        For Each Rng In Range("A1").CurrentRegion
            .Item(Rng.Interior.Color) = Rng & "@" & .Item(Rng.Interior.Color)
        Next Rng
        For i% = 0 To .Count - 1
            Sheet2.Cells(1, i + 1).Resize(UBound(Split(.Item(.keys()(i)), "@"), 1) + 1, 1) = Application.Transpose(Split(.Item(.keys()(i)), "@"))
        Next i
    End With
End Sub

Excel中不同填充颜色的单元格数据导出.rar

25.71 KB, 下载次数: 8

数据导出

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2022-6-6 14:08 | 显示全部楼层
本帖最后由 hasyh2008 于 2022-6-9 13:22 编辑

Sub tt()
  On Error Resume Next
  Application.ScreenUpdating = False '//关闭屏幕刷新
  Application.DisplayAlerts = False '//关闭系统提示
  Application.EnableEvents = False  '//禁止触发其他事件
  Application.StatusBar = False   '关闭系统状态条
  Application.Interactive = False   '禁用鼠标、键盘,防干扰
  Dim D, D2
  Dim Rng As Range
  Dim Ys As Long, Cels As Long
  Dim Rc%, Co%, K%
  Dim Tim As Single
  Tim = Timer
  Dim Arr()
  Set D = CreateObject("scripting.dictionary")
  Set D2 = CreateObject("scripting.dictionary")
  Cels = Sheet1.Range("A1").CurrentRegion.Count     '统计单元格总个数
  For Each Rng In Sheet1.Range("A1").CurrentRegion
    Ys = Rng.Interior.Color
    If D.Exists(Ys) Then
      D2(Ys) = D2(Ys) + 1
      If D2(Ys) > K Then K = D2(Ys)                 '记录数组一维方向的最高值
      Arr(D2(Ys), D(Ys)) = Rng.Value
    Else
      Co = Co + 1
      D(Ys) = Co: D2(Ys) = 1                        'D(Ys):新出现颜色的列数,D2(Ys):行数取1
      ReDim Preserve Arr(1 To Cels, 1 To Co)        '数组一维方向取最大值,二维为准确值
      Arr(1, Co) = Rng.Value
      Sheet2.Cells(1, Co).Interior.Color = Ys       '设置第一行的颜色
    End If
  Next Rng
  Sheet2.Range("A1").Resize(K, Co) = Arr
  MsgBox Format(Timer - Tim, "0.00")
  Set Rng = Nothing
  Application.StatusBar = True   '恢复系统状态条
  Application.EnableEvents = True  '//恢复触发其他事件
  Application.ScreenUpdating = True '//恢复屏幕刷新
  Application.DisplayAlerts = True '//恢复系统提示
  Application.Interactive = True    '启用鼠标键盘
End Sub

Excel中不同填充颜色的单元格数据导出 (20220606).rar

89.96 KB, 下载次数: 1

评分

参与人数 1学分 +2 收起 理由
limonet + 2 学习了

查看全部评分

回复

使用道具 举报

发表于 2022-6-6 15:34 | 显示全部楼层    本楼为最佳答案   
Sub SplitColor()
    Dim Rng As Range
    With CreateObject("scripting.dictionary")
        For Each Rng In Range("A1").CurrentRegion
            .Item(Rng.Interior.Color) = Rng & "@" & .Item(Rng.Interior.Color)
        Next Rng
        For i% = 0 To .Count - 1
            Sheet2.Cells(1, i + 1).Resize(UBound(Split(.Item(.keys()(i)), "@"), 1) + 1, 1) = Application.Transpose(Split(.Item(.keys()(i)), "@"))
        Next i
    End With
End Sub

Excel中不同填充颜色的单元格数据导出.zip

32.77 KB, 下载次数: 6

点评

大写的佩服  发表于 2022-6-6 15:49

评分

参与人数 1学分 +2 收起 理由
zjdh + 2 学习了

查看全部评分

回复

使用道具 举报

发表于 2022-6-6 15:56 | 显示全部楼层
本帖最后由 hasyh2008 于 2022-6-6 16:19 编辑
limonet 发表于 2022-6-6 15:34
Sub SplitColor()
    Dim Rng As Range
    With CreateObject("scripting.dictionary")

得好好学习学习!!!高手都是不走寻常路!!!以前看过这样的做法,一直没用过,今天得消化下!!!!
回复

使用道具 举报

发表于 2022-6-6 16:02 | 显示全部楼层
hasyh2008 发表于 2022-6-6 15:56
得好好学习学习!!!高手都是不走寻常路!!!

不论黑猫白猫,能抓到老鼠就是好猫。
回复

使用道具 举报

发表于 2022-6-6 17:18 | 显示全部楼层
本帖最后由 limonet 于 2022-6-6 17:34 编辑

这个可能更好,排序自然,表头对应颜色
Sub SplitColor()
    Dim Rng As Range
    With CreateObject("scripting.dictionary")
        For Each Rng In Range("A1").CurrentRegion
            .Item(Rng.Interior.Color) = .Item(Rng.Interior.Color) & "@" & Rng
        Next Rng
        For i% = 0 To .Count - 1
            Sheet2.Cells(1, i + 1).Resize(UBound(Split(.Item(.keys()(i)), "@")) + 1) = Application.Transpose(Split(.keys()(i) & .Item(.keys()(i)), "@"))
            Sheet2.Cells(1, i + 1).Interior.Color = .keys()(i)
        Next i
    End With
End Su
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-7 19:03 , Processed in 1.838472 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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