在Excel中排序数据的问题 - CustomOrder参数 [英] Problem sorting data in Excel - CustomOrder parameter

查看:439
本文介绍了在Excel中排序数据的问题 - CustomOrder参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个宏,它应该按照自定义顺序对电子表格中的数据进行排序,以匹配Excel 2007中第二个电子表格中的数据。我面临的问题是它不起作用。我使用的代码是:

I am developing a macro that should sort data in a spreadsheet in custom order to match data in a second spreadsheet in Excel 2007. The problem I am facing is that it does not work. The code I am using is:

    Const cWSMS ="里程碑"

    Const cTblMilestones =" MyDatabase.accdb3"

    Const cWSMS = "Milestones"
    Const cTblMilestones = "MyDatabase.accdb3"

    Dim SortStr As Variant

    Dim SortStr As Variant

    SortStr =" 3,64,24,25,26"

    SortStr = "3,64,24,25,26"

    ActiveWorkbook.Worksheets(cWSMS).ListObjects(cTblMilestones).Sort.SortFields.Add _

       键:=范围(cTblMilestones&" [ProcessID]"),SortOn:= xlSortOnValues,_
$
       订单:= xlAscending,_

        CustomOrder:= SortStr,_

        DataOption:= xlSortNormal       

    ActiveWorkbook.Worksheets(cWSMS).ListObjects(cTblMilestones).Sort.SortFields.Add _
        Key:=Range(cTblMilestones & "[ProcessID]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, _
        CustomOrder:=SortStr, _
        DataOption:=xlSortNormal       

 

如果我使用

   Dim SortStr As String

   Dim SortStr As String



如果添加空格,错误会改变,如下例所示,但仍然无法运行:


The error changes if add spaces, like in the following sentence, but still does not run:

    SortStr =" 3,64,24,25,26"

    SortStr = " 3, 64, 24, 25, 26"



但是,如果我使用常量值更改变量SortStr,它确实有效:


However, if I change the variable SortStr with a constant value, it does work:

    ActiveWorkbook.Worksheets(cWSMS).ListObjects(cTblMilestones).Sort.SortFields.Add _

       键:=范围(cTblMilestones&" [ProcessID]"),SortOn:= xlSortOnValues,_
$
       订单:= xlAscending,_

        CustomOrder:=" 3,64,24,25,26",_

        DataOption:= xlSortNormal

    ActiveWorkbook.Worksheets(cWSMS).ListObjects(cTblMilestones).Sort.SortFields.Add _
        Key:=Range(cTblMilestones & "[ProcessID]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, _
        CustomOrder:="3,64,24,25,26", _
        DataOption:=xlSortNormal

我做错了什么?如何修复它以使用变量而不是常量值?顺便说一句,我不知道为什么我用来创建原始代码的宏添加了3到数据库名称的末尾。谁知道?该文件的真实名称是
MyDatabase.accdb。

What am I doing wrong? How can I fix it to work with variables instead of constant values? By the way, I do not know why the macro I used to create the original code added that 3 to the end of the database name. Anyone know? The real name of the file is MyDatabase.accdb.



谢谢


Thanks

推荐答案

您需要在Excel界面的File>中创建自定义排序顺序。选项>高级>编辑自定义列表...(在"常规"部分中),或使用Application.AddCustomList在VBA中创建它(请参阅
http://msdn.microsoft.com/en-us/library/office/ff196224.aspx  )。

设置CustomOrder参数在您的代码中自定义列表的从一开始的索引。例如,如果您的自定义列表是"编辑自定义列表"对话框中的第7个,请使用CustomOrder:= 7

Set the CustomOrder argument in your code to the one-based index of your custom list. For example, if your custom list is the 7th one in the Edit Custom Lists dialog, use CustomOrder:=7


这篇关于在Excel中排序数据的问题 - CustomOrder参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆