sortowanie danych VBA

0

dostałem następujące zadanie
Napisać program sortujący dane w wierszu arkusza dwoma dowolnymi metodami sortowania
niestety nie umiem sobie poradzic z pobraniem danych z komorek, czy jest ktos w stanie pomoc rozwiazac to zadanie?

0

Function SelectionSort(TempArray As Variant)
Dim MaxVal As Variant
Dim MaxIndex As Integer
Dim i, j As Integer

      For i = UBound(TempArray) To 1 Step -1

          
          MaxVal = TempArray(i)
          MaxIndex = i

          
          For j = 1 To i
              If TempArray(j) > MaxVal Then
                  MaxVal = TempArray(j)
                  MaxIndex = j
              End If
          Next j

          
          If MaxIndex < i Then
              TempArray(MaxIndex) = TempArray(i)
              TempArray(i) = MaxVal
          End If
      Next i

  End Function

  Sub SelectionSortMyArray()
      Dim TheArray As Variant
      TheArray = Array(15, 8, 11, 7, 33, 4, 46, 19, 20, 27, 43, 25, 36)
      SelectionSort TheArray
      For i = 1 To UBound(TheArray)
          MsgBox TheArray(i)
      Next i

  End Sub

Function BubbleSort(TempArray As Variant)
Dim Temp As Variant
Dim i As Integer
Dim NoExchanges As Integer

      ' Loop until no more "exchanges" are made.
      Do
          NoExchanges = True

          ' Loop through each element in the array.
          For i = 1 To UBound(TempArray) - 1

              ' If the element is greater than the element
              ' following it, exchange the two elements.
              If TempArray(i) > TempArray(i + 1) Then
                  NoExchanges = False
                  Temp = TempArray(i)
                  TempArray(i) = TempArray(i + 1)
                  TempArray(i + 1) = Temp
              End If
          Next i
      Loop While Not (NoExchanges)

  End Function

  Sub BubbleSortMyArray()
      Dim TheArray As Variant

      ' Create the array.
      TheArray = Array(15, 8, 11, 7, 33, 4, 46, 19, 20, 27, 43, 25, 36)

      ' Sort the Array and display the values in order.
      BubbleSort TheArray
      For i = 1 To UBound(TheArray)
          MsgBox TheArray(i)
      Next i
  End Sub

i o ile bubble dziala to selectionsort juz nie bardzo, no i tutaj mam dane wpisane z ręki, nie potrafię ich importować z arkusza

0
       Function ArrayFromRange(ByVal r As Range) As Variant
       Dim a As Variant
       Dim c As Range
       Dim i As Long
       
       ReDim a(r.Cells.Count)
       
       For Each c In r.Cells
        i = i + 1
        a(i) = c.Value
       Next
       
       ArrayFromRange = a
        
       End Function

Dodaj tę funkcję. I tam, gdzie masz TheArray=Array(...) zamień na np. TheArray = ArrayFromRange(Range("a1:b5"))
Jest też tam mnóstwo innych błędów, ale nie mam czasu na to.

1 użytkowników online, w tym zalogowanych: 0, gości: 1