为气泡排序创建自定义字符串排序列表

时间:2019-02-11 14:16:55

标签: excel vba

我得到了冒泡排序的要点,但是我正在努力寻找一种方法来实现“字符串1”>“字符串2”。我将从描述我的工作簿开始。

我的工作簿是一个共享的excel文件,用于跟踪每个人的日常项目进度。每天中午,工作簿都会在G列中添加带有日期日期的新列。因此,由于工作簿每天增加一列,因此没有明确的右端。第1至11行被冻结,并填充了非项目特定的信息(例如图例,留言板,其他宏的按钮,列标题)

工作簿中的每个项目占用三行(例如,第12-14行是列出的第一个项目,第15-17行是列出的第二个项目)。列A具有“状态”,“开始日期”和“结束日期”行。我的目的是按状态对项目进行排序。状态单元格具有一个下拉列表,其中包含六个不同的选项:“ R”运行中的毛皮”,“ S”用于设置”,“ H”代表保留,“ P”代表报告,“ U”代表即将到来以及“ C”为完成。这也是我希望对项目进行排序的顺序。

我发现我打算实施here的冒泡排序版本。这篇文章的子内容是:

 '============================================================================
 '- BUBBLE SORT EXAMPLE : 3 NUMBERS (ASCENDING)
 '- The method is to use a "pointer" and check its current position in the array.
 '- If the current number is more than the next then switch their positions in the array.
 '- If a position is changed then set a marker so the pointer goes through again.
 '- The sort is complete when the pointer has gone through the array without change.
 '- Brian Baulsom October 2008  
 '=============================================================================

 Sub SORT_()
     Dim MyNumbers(3)
     Dim Pointer As Integer
     Dim Changed As Boolean
     Dim MyTemp
     '-------------------------------------------------------------------------
     MyNumbers(1) = 30
     MyNumbers(2) = 20
     MyNumbers(3) = 10
     '-------------------------------------------------------------------------
     Do
         Changed = False
         For Pointer = 1 To 3 - 1
             If MyNumbers(Pointer) > MyNumbers(Pointer + 1) Then
                 MyTemp = MyNumbers(Pointer)
                 MyNumbers(Pointer) = MyNumbers(Pointer + 1)
                 MyNumbers(Pointer + 1) = MyTemp
                 Changed = True
             End If
         Next
     Loop While Changed = True
     '-------------------------------------------------------------------------
     MsgBox (MyNumbers(1) & vbCr & MyNumbers(2) & vbCr & MyNumbers(3) & vbCr)
 End Sub
 '============================================================================

为此,我计划更改它如何更改排序后的数字以剪切和粘贴。

我知道我不能只将If“ P”>“ C”放进去而不会引发错误。因此,我现在要弄清楚的是如何给出我可以在仍然(最好)只有一个IF循环的情况下进行排序的每个字符串值。

另存为MyNumbers(1),MyNumbers(2)的数字是否按数字索引或.Value排序?如果按索引排序,那么创建应该没有问题:

 MyNumbers(1) = "R"
 MyNumbers(2) = "S"
 MyNumbers(3) = "H"
 MyNumbers(4) = "P"
 MyNumbers(5) = "U"
 MyNumbers(6) = "C"

但是我对VBA还是不熟悉,无法轻松解决。

0 个答案:

没有答案
相关问题