前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Excel VBA在一个工作簿内把总表拆分多个工作表

Excel VBA在一个工作簿内把总表拆分多个工作表

作者头像
哆哆Excel
发布2022-10-25 12:38:11
2.3K0
发布2022-10-25 12:38:11
举报
文章被收录于专栏:哆哆Excel

知识点:字典,主要是item中可以是union(),并进行复制

======代码如下=======

Sub 在一个工作簿内把总表拆分多个工作表()

Dim title_rng As Range, wb As Object, dic1 As Object

Application.DisplayAlerts = False

Application.ScreenUpdating = False

Set dic1 = CreateObject("scripting.dictionary")

With ActiveSheet

On Error Resume Next

col_rng = Application.InputBox("请输入要拆分的列", , "B", , , , , Type:=2)

If col_rng = "" Then MsgBox "取消了": Exit Sub

'On Error GoTo 0

col_num = Cells(1, col_rng).Column

' MsgBox col_num

Set title_rng = .Rows(1)

endrow = Cells.Find("*", Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious).Row '计算最后一个工作表的非空行号

endCol = Cells.Find("*", Cells(1, 1), xlValues, xlWhole, xlByColumns, xlPrevious).Column '计算最后一个工作表的非空列号

For i = 2 To endrow

s = .Cells(i, col_num).Value

If Not dic1.Exists(s) Then

Set dic1(s) = .Cells(i, 1).Resize(1, endCol)

Else

Set dic1(s) = Union(dic1(s), .Cells(i, 1).Resize(1, endCol))

End If

Debug.Print i

Next i

MsgBox "将拆分出工作表数有:" & dic1.Count

End With

For Each k In dic1.Keys

With ThisWorkbook.Sheets.Add

title_rng.Copy .Range("a1")

dic1(k).Copy .Range("a2")

.Name = k

End With

Next

Application.DisplayAlerts = True

Application.ScreenUpdating = True

End Sub

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 哆哆Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档