2010-08-03 5 views

답변

6

해결책은 다음과 같습니다 (here에서 도난당한 quicksort 코드). 버튼을 SortVals 매크로에 연결하면 단추를 클릭하기 만하면 활성 셀에서 쉼표로 구분 된 값을 정렬합니다.

Option Explicit 

Public Sub SortVals() 
    Dim i As Integer 
    Dim arr As Variant 
    arr = Split(ActiveCell.Text, ",") 

    ' trim values so sort will work properly 
    For i = LBound(arr) To UBound(arr) 
     arr(i) = Trim(arr(i)) 
    Next i 

    ' sort 
    QuickSort arr, LBound(arr), UBound(arr) 

    ' load sorted values back to cell 
    Dim comma As String 
    comma = "" 
    ActiveCell = "" 
    For i = LBound(arr) To UBound(arr) 
     ActiveCell = ActiveCell & comma & CStr(arr(i)) 
     comma = "," 
    Next i 
End Sub 

Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long) 

    Dim pivot As Variant 
    Dim tmpSwap As Variant 
    Dim tmpLow As Long 
    Dim tmpHi As Long 

    tmpLow = inLow 
    tmpHi = inHi 

    pivot = vArray((inLow + inHi) \ 2) 

    While (tmpLow <= tmpHi) 

    While (vArray(tmpLow) < pivot And tmpLow < inHi) 
     tmpLow = tmpLow + 1 
    Wend 

    While (pivot < vArray(tmpHi) And tmpHi > inLow) 
     tmpHi = tmpHi - 1 
    Wend 

    If (tmpLow <= tmpHi) Then 
     tmpSwap = vArray(tmpLow) 
     vArray(tmpLow) = vArray(tmpHi) 
     vArray(tmpHi) = tmpSwap 
     tmpLow = tmpLow + 1 
     tmpHi = tmpHi - 1 
    End If 

    Wend 

    If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi 
    If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi 

End Sub 
+0

'ActiveCell = ActiveCell & comma & CStr (arr (i))'... 그냥'Join'을 사용하십시오! –

2

문자열을 분할하고 값을 정렬하고 정렬 된 값에서 새 문자열을 만들어야합니다.

1

이 질문을 받았을 때 LAselect 프레임 워크 애드 인은 일반인에게 공개되지 않았습니다. 하지만 지금은 그렇습니다. 그리고 이런 종류의 문제를 우아한 방식으로 해결합니다.

VBA에 '솔루션'코드를 몇 줄만 입력하면 원하는 항목을 정렬하거나 주문할 수 있습니다.

쉼표로 분리 된 값을 얻으려면 VBA의 Split() 함수를 사용하십시오. 그런 다음 셀에있는 것과 별개의 값을 가질 수 있습니다. splitvalues ​​(1) FIRSTNAME과 splitvalues ​​(0) LASTNAME 경우, 추가 기능은 다음과 같이 당신의 단일 셀 뭔가 정렬 할 수 있습니다 : LA_ISEQUAL에서

Dim splitvalues1 as Variant, splitvalues2 as Variant 

While LAselect(ControlSignal, BreakOutBox) 
     valcol = BreakOutBox.Pins.ColData 

     splitvalues1 = Split(BreakOutBox.Data.VBArray(BreakOutBox.Pins.RowSource, valcol), ",") 

     splitvalues2 = Split(BreakOutBox.Data.VBArray(BreakOutBox.Pins.RowDestin, valcol), ",") 


     If splitvalues1(0) > splitvalues2(0) Then 
     ControlSignal = LA_ISLARGER 
     ElseIf splitvalues1(0) < splitvalues2(0) Then 
     ControlSignal = LA_ISSMALLER 
     Else 
     ControlSignal = LA_ISEQUAL 
     End If 
Wend 

당신이 splitvalues1을 테스트 할 것을 (1) 등으로 이름에 중복 성을 더 정렬하십시오.

내 추가 기능에 대해 셀 수없이 많은 셀 구분 기호로 구분 된 필드를 넣거나 결정에 포함시킬 다른 셀을 지정하는 것이 중요하지 않다는 것을 분명히 알았습니까? 두 가지 완전한 레코드가 '솔루션'코드로 전달되므로 정렬하려는 속성이 무엇이든 완전히 자유롭게 사용할 수 있습니다.

내 데모는 www.liquorice-allsorts.com에서 찾으실 수 있습니다.

관련 문제