Word宏:如何排除表格排序和显示中的总行;摆脱错误5992 [英] Word Macro: How exclude total row in table sort & Get rid of Error 5992
问题描述
我正在使用宏对具有多个表的Word文件中的表进行排序,虽然效果很好,但可悲的是对总行进行排序,所有表的行数都不相同,排序前后的表分别是:
I am using macro to sort tables in a Word file with multiple tables, it works great but lamentably sort the total row too, all the tables have different row count, tables after and before sort are:
表格示例预排序:
后排序:
宏为:(为此,感谢@scenography)
The macro is: (thanks to @scenography for that)
Dim mytable As Table
For Each mytable In ActiveDocument.Tables
mytable.Sort ExcludeHeader:=True, FieldNumber:="Column 2", _
SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderDescending
Next
任何实现这一目标的方法吗?
Any way to achieve this ?
是否可以避免此行的最后一行(总计)?例如:
Could avoid last row (Total) with this line? example:
Dim last_row As Long last_row = Cells.Find(What:="*", SearchDirection:=xlPrevious).Row )
或
lLastRow = rng(rng.Rows.Count, 1).Row
有可能删除最后一列(Cum),因为排序此值时不正确.
Its possible remove last column(Cum), because when sort this value its not correct.
这里有一个带有Word文档的链接,其中包含我的表格示例以复制结果
Here a link with a word document with example of my tables to replicate results
https://drive.google.com/file/d /1KpV11dHjON_gFnlAvlegzc3pWVfYipV4/view?usp = sharing
推荐答案
不幸的是,您不能从Sort
中排除最后一行",但是,如果它始终是总行",则此解决方法将起作用.
Unfortunately you cannot exclude the Last Row from Sort
, but this workaround will work, if it is always the Total Row.
For i = 1 To ActiveDocument.Tables.Count
Set mytable = ActiveDocument.Tables(i)
mytable.Rows(mytable.Rows.Count).Delete
mytable.Sort ExcludeHeader:=True, FieldNumber:="Column 2", _
SortFieldType:=wdSortFieldNumeric, SortOrder:=wdSortOrderDescending
mytable.Rows(2).Range.Copy
mytable.Rows.Add
mytable.Rows(mytable.Rows.Count).Range.Paste
mytable.Rows(2).Delete
mytable.Cell(1, mytable.Columns.Count).Select
Selection.SelectColumn
Selection.Cells.Delete ShiftCells:=wdDeleteCellsShiftLeft
Next
基本上,我们将排序后的第二行移到最后一行.
Basically we are moving the Second row to the Last row after sorting.
表格如下:
这篇关于Word宏:如何排除表格排序和显示中的总行;摆脱错误5992的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!