ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] Word VBA:判断一个单元格是否为合并单元格

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-5-2 11:26 | 显示全部楼层
请问下为什么我这儿判断不正确呢?

01.png
02.png
判断出错.rar (12.51 KB, 下载次数: 8)


TA的精华主题

TA的得分主题

 楼主| 发表于 2023-5-4 21:09 | 显示全部楼层
shenjianrong163 发表于 2023-5-2 11:26
请问下为什么我这儿判断不正确呢?

前面的代码情况非常复杂时确实有问题
可以试试下面补充方法

  1. Function 是合并单元格_Selection方法(myCell As Cell) As String
  2.     Dim results(1 To 3) As Variant
  3.     Dim tb As Table
  4.     Dim maxRow As Long
  5.     Dim maxCol As Long
  6.     Dim myRow As Long
  7.     Dim myCol As Long
  8.     Dim nextCell As Cell
  9.     Dim i As Long
  10.     Dim 有合并行 As Boolean: 有合并行 = False
  11.     Dim 合并行数 As Long: 合并行数 = 0
  12.     Dim 有合并列 As Boolean: 有合列行 = False
  13.     Dim 合并列数 As Long: 合并列数 = 0
  14.    
  15.     Set tb = myCell.Range.Tables(1)
  16.    
  17.     maxRow = tb.Rows.Count
  18.     maxCol = tb.Columns.Count
  19.     myRow = myCell.RowIndex
  20.     myCol = myCell.ColumnIndex
  21.    
  22.     myCell.Select
  23.     Selection.Collapse
  24.     If myCell.Range.Start >= tb.Range.Cells(tb.Range.Cells.Count).Range.Start Then
  25.         合并列数 = maxCol - myCol + 1
  26.     Else
  27.         Selection.MoveRight wdCell, 1
  28.         If Selection.Cells(1).ColumnIndex > myCol Then
  29.             合并列数 = Selection.Cells(1).ColumnIndex - myCol
  30.         Else
  31.             合并列数 = maxCol - myCol + 1
  32.         End If
  33.     End If
  34.     If 合并列数 > 1 Then
  35.         有合并列 = True
  36.     End If
  37.    
  38.    
  39.     myCell.Select
  40.     Selection.Collapse 0
  41.    
  42.     Selection.MoveDown wdLine
  43.     If Selection.Start >= tb.Range.End Then
  44.         合并行数 = maxRow - myRow + 1
  45.     Else
  46.         合并行数 = Selection.Cells(1).RowIndex - myRow
  47.     End If
  48.     If 合并行数 > 1 Then
  49.         有合并行 = True
  50.     End If
  51.    
  52.     results(1) = 有合并行 Or 有合并列
  53.     results(2) = 合并行数
  54.     results(3) = 合并列数

  55.     是合并单元格_Selection方法 = VBA.Join(results, ";")
  56. End Function

  57. Sub 测试_标题合并单元格情况()
  58.     Dim cel As Cell
  59.     Dim s As String
  60.     For Each cel In ActiveDocument.Tables(2).Range.Cells
  61.         s = 是合并单元格_Selection方法(cel)
  62.         cel.Range.Text = s
  63.         Debug.Print s
  64.     Next
  65. End Sub
复制代码

判断出错.rar

40.47 KB, 下载次数: 6

打开docm文件进行测试

TA的精华主题

TA的得分主题

发表于 2023-5-5 22:56 | 显示全部楼层
守候_CJ 发表于 2023-5-4 21:09
前面的代码情况非常复杂时确实有问题
可以试试下面补充方法

像红色字体单元格应该都存在问题吧?
01.png

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-5-6 15:03 | 显示全部楼层
shenjianrong163 发表于 2023-5-5 22:56
像红色字体单元格应该都存在问题吧?

上面三个单元格判断有问题 其他没问题
代码稍微改了一下:
  1. Function 是合并单元格_Selection方法(myCell As Cell) As String
  2.     Dim results(1 To 3) As Variant
  3.     Dim tb As Table
  4.     Dim maxRow As Long
  5.     Dim maxCol As Long
  6.     Dim myRow As Long
  7.     Dim myCol As Long
  8.     Dim nextCell As Cell
  9.     Dim i As Long
  10.     Dim 有合并行 As Boolean: 有合并行 = False
  11.     Dim 合并行数 As Long: 合并行数 = 0
  12.     Dim 有合并列 As Boolean: 有合列行 = False
  13.     Dim 合并列数 As Long: 合并列数 = 0
  14.    
  15.     Set tb = myCell.Range.Tables(1)
  16.    
  17.     maxRow = tb.Rows.Count
  18.     maxCol = tb.Columns.Count
  19.     myRow = myCell.RowIndex
  20.     myCol = myCell.ColumnIndex
  21.    
  22.     myCell.Select
  23.     Selection.Collapse
  24.     If myCell.Range.Start >= tb.Range.Cells(tb.Range.Cells.Count).Range.Start Then
  25.         合并列数 = maxCol - myCol + 1
  26.     Else
  27.         Selection.MoveRight wdCell, 1
  28.         If Selection.Cells(1).ColumnIndex > myCol Then
  29.             合并列数 = Selection.Cells(1).ColumnIndex - myCol
  30.         Else
  31.             合并列数 = maxCol - myCol + 1
  32.         End If
  33.     End If
  34.     If 合并列数 > 1 Then
  35.         有合并列 = True
  36.     End If
  37.    
  38.    
  39.     myCell.Select
  40.     Selection.Collapse 1
  41.    
  42.     Selection.MoveDown wdLine
  43.     If Selection.Start >= tb.Range.End Then
  44.         合并行数 = maxRow - myRow + 1
  45.     Else
  46.         合并行数 = Selection.Cells(1).RowIndex - myRow
  47.     End If
  48.     If 合并行数 > 1 Then
  49.         有合并行 = True
  50.     End If
  51.    
  52.     results(1) = 有合并行 Or 有合并列
  53.     results(2) = 合并行数
  54.     results(3) = 合并列数

  55.     是合并单元格_Selection方法 = VBA.Join(results, ";")
  56. End Function
复制代码

判断出错.rar

41.73 KB, 下载次数: 5

打开docm文件测试

TA的精华主题

TA的得分主题

发表于 2023-5-22 08:47 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
守候_CJ 发表于 2023-5-6 15:03
上面三个单元格判断有问题 其他没问题
代码稍微改了一下:

关于拼音,可能是拼音的代码库不准确。
Snipaste_2023-05-22_08-42-24.jpg




TA的精华主题

TA的得分主题

 楼主| 发表于 2023-5-22 11:11 | 显示全部楼层
wdpfox 发表于 2023-5-22 08:47
关于拼音,可能是拼音的代码库不准确。

不应该
实测效果:


image.png


TA的精华主题

TA的得分主题

发表于 2023-5-23 11:17 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-5-5 00:19 , Processed in 0.043429 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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