Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel (答案中的ned)或稍微复杂一点的公式(例如,将连字符后的所有内容都视为十进制)。您能给我一个起点来编写自定义排序过程吗?@ChrisProsser……。另一个剧本是替换。对于-以便将破折号部分转换为十进制…这将允许108-1和108-2也正确排序@Gar_Excel_Sorting - Fatal编程技术网

Excel (答案中的ned)或稍微复杂一点的公式(例如,将连字符后的所有内容都视为十进制)。您能给我一个起点来编写自定义排序过程吗?@ChrisProsser……。另一个剧本是替换。对于-以便将破折号部分转换为十进制…这将允许108-1和108-2也正确排序@Gar

Excel (答案中的ned)或稍微复杂一点的公式(例如,将连字符后的所有内容都视为十进制)。您能给我一个起点来编写自定义排序过程吗?@ChrisProsser……。另一个剧本是替换。对于-以便将破折号部分转换为十进制…这将允许108-1和108-2也正确排序@Gar,excel,sorting,Excel,Sorting,(答案中的ned)或稍微复杂一点的公式(例如,将连字符后的所有内容都视为十进制)。您能给我一个起点来编写自定义排序过程吗?@ChrisProsser……。另一个剧本是替换。对于-以便将破折号部分转换为十进制…这将允许108-1和108-2也正确排序@Gary的学生是的,这是一个聪明的解决方案,如果在隐藏列中也这样做的话,会比我的更简单。@mohammad我已经更新了我的答案,也包括了VBA解决方案。 201 202 208-1 210 201 202 210 208-1 201 202 20


(答案中的ned)或稍微复杂一点的公式(例如,将连字符后的所有内容都视为十进制)。您能给我一个起点来编写自定义排序过程吗?@ChrisProsser……。另一个剧本是替换。对于-以便将破折号部分转换为十进制…这将允许108-1和108-2也正确排序@Gary的学生是的,这是一个聪明的解决方案,如果在隐藏列中也这样做的话,会比我的更简单。@mohammad我已经更新了我的答案,也包括了VBA解决方案。
201
202
208-1
210
201
202
210
208-1
201
202
208-1
210
210
208-1
202
201
=IFERROR(VALUE(LEFT(A2,FIND("-",A2)-1)),VALUE(A2))
=IFERROR(VALUE(SUBSTITUTE(A2,"-",".")),VALUE(A2))
Sub sort_with_hyphens()
On Error GoTo sort_with_hyphens_err
   Dim vRange As Range
   Dim vCell As Variant
   Dim vStrArray(), vNumArray()
   Dim i As Long, vStart As Long, vEnd As Long
   Dim vStep As String: vStep = "Initialising values"

   ' prompt user to specify range
   Set vRange = Application.InputBox("Select a range to be sorted", _
                                     "Obtain Range Object", _
                                     Type:=8)
   vStrArray = vRange.Value
   vStart = LBound(vStrArray)
   vEnd = UBound(vStrArray)
   ReDim vNumArray(vStart To vEnd)
   vStep = "Populating Numeric Array"

   ' loop through array copying strings with hyphen to decimal equivalent
   For i = vStart To vEnd
       vNumArray(i) = Val(Replace(vStrArray(i, 1), "-", "."))
       Debug.Print i, vNumArray(i)
   Next i

   ' sort numeric array
   vStep = "Sorting Numeric Array"
   SortViaWorksheet vNumArray

   ' write out sorted values
   vStep = "Writing out Sorted Values"
   For i = vStart To vEnd
       ' convert back to string and switch periods back to hyphens
       vRange.Cells(i, 1).Value = Replace(CStr(vNumArray(i)), ".", "-")
   Next

sort_with_hyphens_exit:
    Exit Sub

sort_with_hyphens_err:
    If vStep = "Writing out Sorted Values" Then
        MsgBox ("An error has occurred, the original values will " & _
                "be restored. Error in Step: " & vStep & vbCrLf & _
                "Error Details:" & vbCrLf & err.Number & " - " & _
                err.Description)
        For i = vStart To vEnd
            ' replace with original value incase of error
            vRange.Cells(i, 1).Value = vStrArray(i)
        Next
    Else
        MsgBox ("An error has occurred in Step: " & vStep & vbCrLf & _
                "Aborting sort procedure." & vbCrLf & _
                "Error Details:" & vbCrLf & err.Number & " - " & _
                err.Description)
   End If
End Sub

Sub SortViaWorksheet(pArray)
    Dim WS As Worksheet ' temporary worksheet
    Dim R As Range
    Dim N As Long

    Application.ScreenUpdating = False

    ' create a new sheet
    Set WS = ThisWorkbook.Worksheets.Add

    ' put the array values on the worksheet
    Set R = WS.Range("A1").Resize(UBound(pArray) - LBound(pArray) + 1, 1)
    R = Application.Transpose(pArray)

    ' sort the range
    R.Sort key1:=R, order1:=xlAscending, MatchCase:=False

    ' load the worksheet values back into the array
    For N = 1 To R.Rows.Count
        pArray(N) = R(N, 1)
    Next N

    ' delete the temporary sheet
    Application.DisplayAlerts = False
    WS.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    ' test/debug/confirmation
    Debug.Print vbCrLf & "Sorted Array:" & vbCrLf & "------------"
    For N = LBound(pArray) To UBound(pArray)
        Debug.Print N, pArray(N)
    Next N
End Sub