问题描述
当我使用宏记录器对ListObject(表)进行排序时,它会产生:
When I use the macro recorder on sorting a ListObject (table), it produces:
ActiveWorkbook.Worksheets("Output").ListObjects("Table4").Sort.SortFields.Add2
在我的Excel版本中可以很好地工作.但是,这不能在其他Excel上编译.将其更改为.Add
即可解决问题.
which works perfectly fine in my version of Excel. However, this doesn't compile on other Excels. Changing it to .Add
solves the problem.
Google表示.Add2
仅存在于ChartObjects中-但记录器为ListObject记录了它-并且它在这里有效,但在其他Excel(相同版本)中则无效.
Google says that .Add2
only exists for ChartObjects - but the recorder records it for a ListObject - and it works here but not in the other Excel (same version).
什么是.Add2
?为什么我的Excel认为这是正确的方法?
What is .Add2
? Why does my Excel think that's the correct way?
推荐答案
这不是答案;我想补充一下我发现的内容.也许应该(但无法)使用评论.
This is not an answer; I'd like to add what I found out. Should have used comment maybe (but unable to).
- 如果您在ListObject或Worksheet上定义过滤器,我可以确认 Excel 365商业宏记录器记录
Sort.SortFields.Add2
. - 我不能不确认该宏在另一台计算机上的同一Excel版本上不起作用(对我而言,它可以正常工作;已使用Excel 16.0.10228.20080 32Bit测试).
-
Add2
方法未记录在MSDN上的SortFields
集合( 1 ),但对于其他服务器对象(因此仅适用于ChartObject"不正确). - VBA对象库(我的Excel版本,请参见上文)在
SortFields
类中将Add2
列出为Function;Add
和Add2
之间的唯一区别似乎是Add2
还有一个可选参数[SubField]
.
- I can confirm Excel 365 Business macro recorder records
Sort.SortFields.Add2
if you define a filter on either a ListObject or a Worksheet. - I can not confirm the macro does not work on same Excel version on another machine (for me it works; tested with Excel 16.0.10228.20080 32Bit).
- The
Add2
method is not documented on MSDN for theSortFields
Collection (1), but for serveral other objects (so "only for ChartObject" is not corrrect). - The VBA object library (of my Excel version, see above) lists
Add2
as Function inSortFields
Class; the only difference betweenAdd
andAdd2
seems to be thatAdd2
has one more optional paramter[SubField]
.
结论:我想(但这只是一个猜测!)Add2
to是功能的扩展,出于兼容性原因,该功能已使用新名称实现.在这种情况下,宏记录器可能总是使用最新功能.除非需要SubField
参数,否则您可以使用Add
.
Conclusion: I guess (but this is only a guess!) that Add2
to is an expansion in functionality that has been implemented with a new name for compatibility reasons. Maybe the macro recorder always uses the most recent function in such a case.Yor can use Add
unless you need the SubField
parameter.
我仍然想加深了解,就像埃德温·埃德勒(Edwin Ederle)要求的那样.由于完全相同的情况,我碰到了这篇文章:使用记录器来学习编码排序功能,对Add2
感到疑惑,并询问google.如果您搜索"vba sortfields add2" =>,那么这个问题是第一个Google匹配(其中之一)=>也许值得在此处提供更多信息.
I still like to get a deeper understanding, like Edwin Ederle asked for. I came across this post because of exact the same situation: using the recorder to learn about coding sort functions, wondering about Add2
, and asking google. This question is (one of) the first google hit(s) if you search for "vba sortfields add2" => maybe worth giving some more information here.
这篇关于什么是sort2的add2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!