Excel精英培训网

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

[已解决]怎么加添下代码,让生成数据多一项内容

[复制链接]
发表于 2016-6-28 10:33 | 显示全部楼层 |阅读模式
本帖最后由 mathking77 于 2016-6-28 15:10 编辑

我想把J列的数据(部门名称),加添到生成后的Q列
原先的代码表格里已经有了,需要加添下,自己照着写老是不对

请教下



最佳答案
2016-6-28 14:43
mathking77 发表于 2016-6-28 13:12

Sub xx()
Dim arr, d, n&, brr()
    Set d = CreateObject("Scripting.Dictionary")
    With Sheet1
        n = .Cells(.Rows.Count, 1).End(xlUp).Row
        arr = .Range("A2:J" & n)
        ReDim brr(i To n)
        For i = 1 To n - 1
            If Not d.Exists(arr(i, 7)) Then
                d.Add arr(i, 7), arr(i, 7)
                j = j + 1
                brr(j) = arr(i, 10)
               
                Set d(arr(i, 7)) = CreateObject("Scripting.Dictionary")
            End If
            If arr(i, 2) <= TimeValue("14:00:00") Then
                If d(arr(i, 7)).Exists("中餐次数") Then
                    d(arr(i, 7))("中餐次数") = d(arr(i, 7))("中餐次数") + 1
                Else
                    d(arr(i, 7))("中餐次数") = 1
                End If
            Else
                If d(arr(i, 7)).Exists("晚餐次数") Then
                    d(arr(i, 7))("晚餐次数") = d(arr(i, 7))("晚餐次数") + 1
                Else
                    d(arr(i, 7)).Add "晚餐次数", 1
                End If
            End If
        Next
        x = 1
        .Cells(x, 14) = "姓名"
        .Cells(x, 15) = "中餐次数"
        .Cells(x, 16) = "晚餐次数"
        For Each k In d.keys
            x = x + 1
            .Cells(x, 14) = k
            .Cells(x, 15) = d(k)("中餐次数")
            .Cells(x, 16) = d(k)("晚餐次数")
            .Cells(x, 17) = brr(x - 1)
        Next
    End With
End Sub
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2016-6-28 13:12 | 显示全部楼层
回复

使用道具 举报

发表于 2016-6-28 14:43 | 显示全部楼层    本楼为最佳答案   
mathking77 发表于 2016-6-28 13:12

Sub xx()
Dim arr, d, n&, brr()
    Set d = CreateObject("Scripting.Dictionary")
    With Sheet1
        n = .Cells(.Rows.Count, 1).End(xlUp).Row
        arr = .Range("A2:J" & n)
        ReDim brr(i To n)
        For i = 1 To n - 1
            If Not d.Exists(arr(i, 7)) Then
                d.Add arr(i, 7), arr(i, 7)
                j = j + 1
                brr(j) = arr(i, 10)
               
                Set d(arr(i, 7)) = CreateObject("Scripting.Dictionary")
            End If
            If arr(i, 2) <= TimeValue("14:00:00") Then
                If d(arr(i, 7)).Exists("中餐次数") Then
                    d(arr(i, 7))("中餐次数") = d(arr(i, 7))("中餐次数") + 1
                Else
                    d(arr(i, 7))("中餐次数") = 1
                End If
            Else
                If d(arr(i, 7)).Exists("晚餐次数") Then
                    d(arr(i, 7))("晚餐次数") = d(arr(i, 7))("晚餐次数") + 1
                Else
                    d(arr(i, 7)).Add "晚餐次数", 1
                End If
            End If
        Next
        x = 1
        .Cells(x, 14) = "姓名"
        .Cells(x, 15) = "中餐次数"
        .Cells(x, 16) = "晚餐次数"
        For Each k In d.keys
            x = x + 1
            .Cells(x, 14) = k
            .Cells(x, 15) = d(k)("中餐次数")
            .Cells(x, 16) = d(k)("晚餐次数")
            .Cells(x, 17) = brr(x - 1)
        Next
    End With
End Sub
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-14 11:46 , Processed in 0.191653 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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