【问题标题】:Sorting in excel using sub, sorting not proper使用sub在excel中排序,排序不正确
【发布时间】:2015-08-26 18:15:40
【问题描述】:

我想对工作表中包含测试用例 ID 的特定列进行排序, 我当前的排序子(如下所示)正在对它进行排序

 1.1.1
 1.1.12
 1.1.15
 1.1.2
 1.1.22
 1.1.3

是因为它把它当作字母数字数据吗?

如何让它正确排序?

1.1.1
1.1.2
1.1.3
1.1.12
1.1.15
1.1.22

我的子功能:

    Function sortSheet(ByVal sheet As Excel.Worksheet)
    Dim oneRange As Range
    Dim aCell As Range
    Dim lastRow As Long, lastCol As Integer

   'Calculating the last row and column
   lastRow = sheet.Cells(Rows.Count, 1).End(xlUp).row
   lastCol = sheet.Cells(1, Columns.Count).End(xlToLeft).Column

   'Setting the range in which sorting is to be done
    Set oneRange = sheet.Range(sheet.Cells(2, 1), sheet.Cells(lastRow, lastCol))

   'Setting the range according to which it will be sorted
    Set aCell = sheet.Range("A2")
   'Sorting
    oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlGuess

    End Function

【问题讨论】:

  • 在工作表上,您将使用辅助列。在 VBA 中,您必须将其放入数组中,将数组中的每个元素拆分为任何元素所拥有的最大数量的片段,然后对数组的数组进行排序并将其放回工作表中。
  • 顺便说一句,有Header:=xlYesHeader:=xlNo 选项。让 Excel 猜测可能不是一个好主意,因为您应该知道您的数据是否有标题。
  • 11.1 是多级列表完整系列的一部分吗? “件”的最大数量是多少?
  • 现在我的 id 会像 1.1.1 , 1.1.12 没有级别,我的测试用例 id 的格式是这样的,我尝试了帮助列它仍然以相同的方式排序。

标签: excel sorting vba


【解决方案1】:

帮助列中的正确公式可以快速解决此问题。

            

B2中的公式是,

=TEXT(LEFT(A2, FIND(".", A2)-1), "000")&TEXT(MID(A2, FIND(".", A2)+1, 9), "000")&TEXT(MID(A2, FIND(".", A2, FIND(".", A2)+1)+1, 9), "000")

根据需要填写。这是有效的,因为多级序数存在静态数量的“块”。如果必须容纳 1、2 或 3 个部分,公式会更复杂。

【讨论】:

    猜你喜欢
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2021-12-07
    • 2015-01-13
    相关资源
    最近更新 更多