Excel VBA自定义订单排序

时间:2018-10-17 06:44:11

标签: excel vba sorting

嗨,我想根据值(临界,高,中,低)对整个C列进行排序。我正在启用宏的工作表上运行此代码

这是我的代码。

Sub run()
Range("C:C").Sort Key1:=Range("C1"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="Critical,High,Medium,Low", DataOption:=xlSortNormal
End Sub

它没有工作,因为指出了错误。没有争论解决此问题的解决方案是什么?谢谢。

2 个答案:

答案 0 :(得分:1)

您的自定义排序条件必须位于数组中。试试

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour
{

    private ParticleSystem muzzleFlash;
    Vector3 tipOfGun = new Vector3(0.009f, 0.329f, 0.017f);

    // Use this for initialization
    void Start()
    {
        GameObject muzzleFlashObj = GameObject.Find("muzzleFlash");
        muzzleFlash = muzzleFlashObj.GetComponent<ParticleSystem>();
    }



    public void ShootWeapon()
    {

        muzzleFlash.Play();
        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;
        if (Physics.SphereCast(ray, 0.75f, out hit))
        {
            Debug.Log("Name of component hit:" + hit.collider.gameObject.name);

            GameObject hitObject = hit.transform.gameObject;
            if (hitObject.GetComponent<PlayerController>())
            {
                muzzleFlash.Play();
            }
            else
                muzzleFlash.Stop();
        }

    }


}

如果在公共模块中,则合格的父级工作表引用会有所帮助。

答案 1 :(得分:0)

尝试:

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("A:A") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
    "Critical,High,Medium,Low", DataOption:=xlSortNormal

    With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange Range("A:A")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With