Excel精英培训网

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

[已解决]VBA求助

[复制链接]
发表于 2013-10-9 14:46 | 显示全部楼层 |阅读模式
本帖最后由 wxfwxj 于 2013-10-10 11:19 编辑

新建 Microsoft Excel 工作表.zip (7.14 KB, 下载次数: 24)
发表于 2013-10-9 15:18 | 显示全部楼层
你都知道合并单元格够坑人的了,自己输出还要用合并单元格。
回复

使用道具 举报

 楼主| 发表于 2013-10-9 15:30 | 显示全部楼层
回复

使用道具 举报

发表于 2013-10-9 15:45 | 显示全部楼层
其实用技巧应该也是可以的。如果实际应用中组的数量大于2个的话,那又不一样了。
回复

使用道具 举报

发表于 2013-10-9 15:53 | 显示全部楼层
Public Sub 测试()
Dim h As Integer, ra As Range, st1 As String, s1 As Integer, x As Integer, y As Integer
st1 = [a2]
Set ra = Sheets("Sheet2").[a:a]
h = Application.Match(st1, ra, 0)
s1 = Sheets("Sheet2").Range("A" & h).MergeArea.Count
y = 3
For x = h To h + s1 - 1
If Sheets("Sheet2").Cells(x, 2) <> "" Then
Cells(y + 1, 1) = Sheets("Sheet2").Cells(x, 2)
y = y + 1
End If
Next
End Sub

以第一组为列,自己再加吧
回复

使用道具 举报

 楼主| 发表于 2013-10-9 16:06 | 显示全部楼层
wangzan 发表于 2013-10-9 15:53
Public Sub 测试()
Dim h As Integer, ra As Range, st1 As String, s1 As Integer, x As Integer, y As I ...

不要每次都运行,可以吗,
就是说表2中姓名增加了表3中也自动增加不要去再运行宏
回复

使用道具 举报

发表于 2013-10-9 16:12 | 显示全部楼层    本楼为最佳答案   
  1. Sub 整理()
  2.     Dim arr, arr2
  3.     Dim strKey$
  4.     Dim i&, j&
  5.     Dim d As Object
  6.    
  7.     arr = Sheet2.Range("a1").CurrentRegion.Value
  8.     Set d = CreateObject("scripting.dictionary")
  9.     For i = LBound(arr) + 1 To UBound(arr)
  10.         If Len(arr(i, 1)) Then strKey = arr(i, 1)
  11.         If Len(arr(i, 2)) Then d(strKey) = d(strKey) & arr(i, 2) & "#"
  12.     Next
  13.    
  14.     If d.Count = 0 Then Exit Sub
  15.     Worksheets.Add
  16.    
  17.     arr2 = d.keys
  18.     Application.ScreenUpdating = False
  19.     For i = 1 To d.Count
  20.         j = (i - 1) * 3 + 1
  21.         With Cells(2, j)
  22.             .Value = arr2(i - 1)
  23.             .Offset(1).Value = "姓名"
  24.             .Resize(, 3).HorizontalAlignment = xlCenterAcrossSelection
  25.             arr = WorksheetFunction.Transpose(Split(d(arr2(i - 1)), "#"))
  26.             With .Offset(2).Resize(UBound(arr) - 1)
  27.                 .Value = arr
  28.                 .Interior.ColorIndex = 6
  29.             End With
  30.         End With
  31.     Next
  32.     Application.ScreenUpdating = True
  33.     MsgBox "OK"
  34. End Sub
复制代码
回复

使用道具 举报

发表于 2013-10-9 16:14 | 显示全部楼层
结果在新表中输出,自己根据情况来改。
回复

使用道具 举报

 楼主| 发表于 2013-10-9 16:22 | 显示全部楼层
hwc2ycy 发表于 2013-10-9 16:12

运行后就自动插入了一个工作表

点评

我说了结果在新表中输出嘛。  发表于 2013-10-9 16:26
回复

使用道具 举报

发表于 2013-10-9 16:45 | 显示全部楼层
VBA-字典 合并单元格-整理 新建 Microsoft Excel 工作表.rar (15.02 KB, 下载次数: 3)
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 06:02 , Processed in 0.666320 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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