ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 753|回复: 3

[讨论] 自己写的合并单元格处理宏,请各位大侠提点意见

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-12-27 13:04 | 显示全部楼层 |阅读模式
最近因为工作关系,经常需要处理各种合并单元格的问题,自己用vba写了一个处理的宏,不过后来想起来搜了一下,好像很少有用两层for循环这种思路来写的,请问是我的思路有问题吗,还是这种做法会有什么效率上的问题,请各位提下意见。

  1. Attribute VB_Name = "模块1"
  2. Sub 当前列单元格合并拆分()
  3. Attribute 当前列单元格合并拆分.VB_ProcData.VB_Invoke_Func = " \n14"
  4. '
  5. '当前列单元格合并拆分
  6. '
  7. '
  8. Application.ScreenUpdating = False
  9. Application.DisplayAlerts = False
  10. crtcol = Selection.Column
  11. colheight = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Row - 1
  12. 'MsgBox IsEmpty(Cells(3461, 3))
  13. userinput = CInt(InputBox("请选择功能:" & vbCrLf & "1为所在列相同单元格自动合并" & vbCrLf & "2为所在列空单元格向上合并" & vbCrLf & "3为所在列合并单元格拆分并填充" & vbCrLf & "4为所在列合并单元格拆分不填充", "功能选择"))
  14. If userinput = 1 Then
  15.     Call colMerge(crtcol, colheight)
  16.     ElseIf userinput = 2 Then
  17.     Call nullMerge(crtcol, colheight)
  18.     ElseIf userinput = 3 Or userinput = 4 Then
  19.     Call colUnMerge(crtcol, colheight, userinput)
  20.     Else
  21.     MsgBox "该模块尚未开发,敬请期待"
  22. End If
  23. Application.ScreenUpdating = True
  24. Application.DisplayAlerts = True
  25.     'MsgBox crtcol
  26.     'Call colUnMerge(crtcol, colheight)
  27.     'Call colMerge(crtcol, colheight)
  28. End Sub

  29. Private Sub colUnMerge(crtcol, colheight, userinput)
  30. For i = 1 To colheight
  31.     With Cells(i, crtcol).MergeArea
  32.     'MsgBox .Address
  33.     If (.Columns.Count = 1) And (.Rows.Count > 1) Then
  34.     mgheight = .Rows.Count
  35.     .UnMerge
  36.     If userinput = 3 Then
  37.     .Value = Cells(i, crtcol).Value
  38.     End If
  39.     i = i + mgheight - 1
  40.     End If
  41.     End With
  42. Next
  43. End Sub

  44. Private Sub colMerge(crtcol, colheight)

  45. For i = 1 To colheight
  46.     'If i > 40 Then
  47.     'MsgBox i
  48.     'End If
  49.     If Cells(i, crtcol).MergeCells Then
  50.     i = i + Cells(i, crtcol).MergeArea.Rows.Count - 1
  51.     ElseIf IsEmpty(Cells(i, crtcol)) Then
  52.    
  53.     Else
  54.     j = i
  55.     tmp = Cells(i, crtcol).Value
  56.     Do
  57.     j = j + 1
  58.     Loop While (Cells(j, crtcol).Value = tmp) And (j <= colheight)
  59.     j = j - 1
  60.     If j > i Then
  61.         Range(Cells(i, crtcol), Cells(j, crtcol)).Merge
  62.     End If
  63.     i = j
  64.     End If
  65. Next
  66.     With Columns(crtcol)
  67.         .HorizontalAlignment = xlCenter
  68.         .VerticalAlignment = xlCenter
  69.         .WrapText = False
  70.     End With
  71. End Sub



  72. Private Sub nullMerge(crtcol, colheight)
  73. For i = 1 To colheight
  74.     'If i > 40 Then
  75.     'MsgBox i
  76.     'End If
  77.     If Cells(i, crtcol).MergeCells Then
  78.     i = i + Cells(i, crtcol).MergeArea.Rows.Count - 1
  79.     Else
  80.     j = i
  81.     Do
  82.         j = j + 1
  83.     Loop While IsEmpty(Cells(j, crtcol)) And (j <= colheight)
  84.     j = j - 1
  85.     If j > i Then
  86.         Range(Cells(i, crtcol), Cells(j, crtcol)).Merge
  87.     End If
  88.     i = j
  89.     End If
  90. Next
  91.     With Columns(crtcol)
  92.         .HorizontalAlignment = xlCenter
  93.         .VerticalAlignment = xlCenter
  94.         .WrapText = False
  95.     End With
  96. End Sub
复制代码


TA的精华主题

TA的得分主题

发表于 2021-12-28 15:29 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
拆分和合并单元格也能写出这么多代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-12-29 09:16 | 显示全部楼层
llllzh 发表于 2021-12-28 15:29
拆分和合并单元格也能写出这么多代码

额,有四个功能,相同的合并,或者和下面的空白单元格合并,拆了,和拆了之后每个单元格都填上同样的内容

TA的精华主题

TA的得分主题

发表于 2021-12-29 11:01 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-6-2 23:30 , Processed in 0.031186 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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