Sheet의 좌측 rows 인덱스 번호를 클릭해서 선택된 rows 번호만 가져오는 함수.

BubbleSort 는 Microsoft VisualBasic 도움말 페이지에서 가져온 것.


'_____________________________________________________
'선택된 rows가 없으면 GetSelectedRows()는 False 반환,
'선택된 rows가 있으면 GetSelectedRows()는 Integer Array 반환 (정렬도 함)
'_____________________________________________________

'선택된 rows 가져오기
Function GetSelectedRows() As Variant
    Dim i, j As Integer
    Dim strArr1() As String
    Dim strArr2() As String
    Dim ResultStr, Delimiter As String
    Dim TempStrArr() As String
    Dim ResultArr() As Integer
    Dim RowNum1, RowNum2 As String
    
    ResultStr = ""
    
    '기본 반환값은 False
    GetSelectedRows = False
        
    strArr1 = Split(Application.Selection.Address, ",")
    For i = LBound(strArr1) To UBound(strArr1)
        '$숫자:$숫자 형태로 되어 있어야 row 선택
        If InStr(1, strArr1(i), ":", vbTextCompare) Then
            strArr2 = Split(strArr1(i), ":")
            
            RowNum1 = GetRowNumber(strArr2(0))
            RowNum2 = GetRowNumber(strArr2(1))
            If RowNum1 <> "" And RowNum2 <> "" Then
                For j = CInt(RowNum1) To CInt(RowNum2)
                    If ResultStr <> "" Then
                        Delimiter = "|"
                    Else
                        Delimiter = ""
                    End If
                
                    ResultStr = ResultStr & Delimiter & j
                Next j
            End If
        End If
    Next
    
    '선택된 row가 있을 경우 반환값은 Integer 배열로 세팅
    If ResultStr <> "" Then
        TempStrArr = Split(ResultStr, "|")
        
        ReDim Preserve ResultArr(0 To UBound(TempStrArr))
        For i = LBound(TempStrArr) To UBound(TempStrArr)
            ResultArr(i) = CInt(TempStrArr(i))
        Next i
        
        BubbleSort ResultArr
        GetSelectedRows = ResultArr
    End If
End Function

'숫자로만 되어있는 row 번호 반환
Function GetRowNumber(strRange)
    Dim i As Integer
    Dim CharAt As String
    
    GetRowNumber = ""
    
    For i = 1 To Len(strRange)
        CharAt = Mid(strRange, i, 1)
        If (CharAt >= "0" And CharAt <= "9") Or CharAt = "$" Then
            If CharAt <> "$" Then GetRowNumber = GetRowNumber + CharAt
        Else
            GetRowNumber = ""
            Exit Function
        End If
    Next i
End Function

'정렬
Function BubbleSort(ArrRows As Variant)
    Dim Temp As Variant
    Dim i As Integer
    Dim NoExchanges As Integer
    
    Do
        NoExchanges = True
        
        For i = 0 To UBound(ArrRows) - 1
            If ArrRows(i) > ArrRows(i + 1) Then
                NoExchanges = False
                Temp = ArrRows(i)
                ArrRows(i) = ArrRows(i + 1)
                ArrRows(i + 1) = Temp
            End If
        Next i
    Loop While Not (NoExchanges)
End Function













Posted by bloodguy
,