本帖最后由 leroy 于 2013-5-13 21:32 编辑
限于个人水平,这里借助VBA中的普通数组和循环来解(没有对计算结果排序,不知道行不行?{:soso_e103:}),
- Sub OrderNumber_Collection()
- Dim i&, j&, k&, arr, result
- Dim temp
- With Sheets("第一题(VBA)")
- arr = .Range(.[A1], .[A65536].End(3))
- ReDim result(1 To UBound(arr), 1 To 1)
- For i = 1 To UBound(arr)
- '第一步,解决"-"
- If InStr(arr(i, 1), "-") > 0 Then
- Do
- j = InStr(arr(i, 1), "-")
- temp = ""
- If Mid(arr(i, 1), j - 2, 2) Like "##" And Mid(arr(i, 1), j + 1, 2) Like "##" Then
- For k = Mid(arr(i, 1), j - 2, 2) To Mid(arr(i, 1), j + 1, 2)
- temp = temp & k & "/"
- Next
- arr(i, 1) = Replace(Replace(arr(i, 1), Mid(arr(i, 1), j - 2, 5), temp), "//", "/")
- End If
- Loop While InStr(arr(i, 1), "-") > 0
- End If
- '第二步,解决"/"
- If InStr(10, arr(i, 1), "/") > 0 Then
- j = 9
- Do
- j = InStr(j + 1, arr(i, 1), "/")
- If Mid(arr(i, 1), j + 1, 2) Like "##" And Mid(arr(i, 1), j - 9, 9) Like "#########" Then
- If Mid(arr(i, 1), j + 3, 1) Like "#" = False Or Len(arr(i, 1)) = j + 2 Then
- arr(i, 1) = Left(arr(i, 1), j) & Mid(arr(i, 1), j - 9, 7) & Mid(arr(i, 1), j + 1, Len(arr(i, 1)))
- j = j + 7
- End If
- End If
- Loop While InStr(j + 1, arr(i, 1), "/") > 0
- End If
- '第三步,提取连续的9位数字
- For j = 1 To Len(arr(i, 1)) - 8
- If Mid(arr(i, 1), j, 9) Like "#########" Then
- If Len(result(i, 1)) = 0 Then
- result(i, 1) = Mid(arr(i, 1), j, 9)
- Else
- result(i, 1) = result(i, 1) & " " & Mid(arr(i, 1), j, 9)
- End If
- End If
- Next
- Next
- .[B:B].ClearContents
- .[B1].Resize(UBound(arr)) = result
- End With
- End Sub
复制代码
|