使用VBA在Excel中进行字母数字排序

时间:2016-08-17 15:26:38

标签: excel vba excel-vba sorting alphanumeric

我在导入到另一个程序之前在Excel中使用创建物料清单,我试图通过参考名称对这些列表进行排序,其中包括前缀页码1-200,后跟设备类型A-ZZ,后跟设备号(ID)在1-99页上,后跟代表设备AZ一部分的字母(如果该设备由多个部分组成)。
以下是RefDes的例子:

1Q1
1S6
1S7
1T1
1VENT
1X1
1X2
1Y1
1Z1-A
1Z1-B
2A1-A
2A1-B
2A1-C
22M1
2QF1
2RB1
2Z1-A
2Z1-B
13A1-A
13A1-B
13A1-C
3A2-A
3A2-B
3A2-C

我想首先按设备类型A-ZZ排序,然后按设备ID排序,然后按设备部件排序,再按页面排序。

1A1A
....
1A1Z
1A2A
....
1A2Z
2A1A
....
2A1Z
....
200A99Z
1B1A
....
200ZZ99Z

这样上面的列表就像这样排序:

2A1-A
2A1-B
2A1-C
3A1-A
3A1-B
3A1-C
13A1-A
13A1-B
13A1-C
22M1
1Q1
2QF1
2RB1
1S6
1S7
1T1
1VENT
1X1
1X2
1Z1-A
1Z1-B
2Z1-A
2Z1-B

到目前为止,我已经能够按页面排序,然后按设备类型进行排序,即使这样也排序错误:1A-1ZZ,然后是1xA-1xZZ,然后是2A-2ZZ,然后是2xA-2xZZ等...我可以得到在构建BOM时摆脱破折号( - )。

1 个答案:

答案 0 :(得分:2)

我不知道这是否是最有效的方式,但根据BOM的构建方式,将数据拆分为临时列可能更容易,然后对其进行排序,然后删除列。

您的排序要求和顺序似乎过于复杂,无法以我所知的其他方式执行此操作。

Sub SortBOMS()
Dim workingRange As Range
Dim workingCell As Range
Dim pageNumber As String
Dim deviceType As String
Dim deviceID As String
Dim devicePart As String
Dim i As Integer

Application.ScreenUpdating = False

'Obtains the full list. (Assumes you have no data after the 1millionth row)
Set workingRange = Range("A1:A" & Range("A1000000").End(xlUp).Row)

For Each workingCell In workingRange.Cells

    'Builds the page number
    pageNumber = ""
    For i = 1 To 3  'Used 3 since 200 is 3 characters
        If IsNumeric(Mid(workingCell.Text, i, 1)) Then
            pageNumber = pageNumber & Mid(workingCell.Text, i, 1)
        Else          
            Exit For    'Exits as soon as encounters a letter
        End If
    Next i
    'Writes the value after converting it to an integer
    Range("B" & workingCell.Row).Value = CInt(pageNumber)

    'Builds the device type
    deviceType = ""
    For i = 1 To 2  'Used 2 since ZZ is 2 characters
        If Not (IsNumeric(Mid(Split(workingCell.Text, pageNumber)(1), i, 1))) Then
            deviceType = deviceType & Mid(Split(workingCell.Text, pageNumber)(1), i, 1)
        Else
            Exit For    'Exits as soon as encounters a number
        End If
    Next i
    'Writes the value
    Range("C" & workingCell.Row).Value = deviceType

    'Builds the device ID
    deviceID = ""
    For i = 1 To 2  'Used 2 since 99 is 2 characters
        If IsNumeric(Mid(Split(workingCell.Text, pageNumber & deviceType)(1), i, 1)) Then
            deviceID = deviceID & Mid(Split(workingCell.Text, pageNumber & deviceType)(1), i, 1)
        Else    
            Exit For    'Exits as soon as encounters a letter or a dash
        End If
    Next i
    'Writes the value after converting it to an integer
    On Error Resume Next
    Range("D" & workingCell.Row).Value = CInt(deviceID)
    On Error GoTo 0

    'Builds the device part
    devicePart = ""
    If InStr(1, workingCell.Text, "-", vbTextCompare) > 0 Then
        devicePart = Split(workingCell.Text, "-")(1)
    End If
    'Writes the value
    Range("E" & workingCell.Row).Value = devicePart
Next workingCell

'Clean up
Application.ScreenUpdating = True
Set workingRange = Nothing
Set workingCell = Nothing
End Sub

运行之后,您应该能够按照自己的方式进行排序。另外,要获得您想要按此顺序排序的结果,而不是您在帖子中提到的顺序:

  1. 设备类型
  2. 页码
  3. 设备ID
  4. 设备部件