Sub BubbleSort() Dim rowCount As Long Dim colCount As Integer Dim temp As Integer Dim i…
- User
Sub BubbleSort() Dim rowCount As Long Dim colCount As Integer Dim temp As Integer Dim i As Integer, j As Integer
colCount = WorksheetFunction.CountA(Range(“A2:A” & (Cells(Rows.count, “A”).End(xlUp).Row) ))
For i = 1 To colCount - 1 For j = 1 To colCount - i If Cells(j, 1).Value > Cells(j + 1, 1).Value Then temp = Cells(j, 1).Value Cells(j, 1).Value = Cells(j + 1, 1).Value Cells(j + 1, 1).Value = temp
End If
Next j Next i
End Sub последний непустой элемент в столбике никак не затрагивается в сортировке, и ячейка A1 по прежнему участвует в сортировке, когда в ней должн быть название столбика. Исправь это
- Assistant
Sub BubbleSort() Dim rowCount As Long Dim colCount As Integer Dim temp As Integer Dim i As Integer, j As Integer
colCount = WorksheetFunction.CountA(Range("A2:A" & (Cells(Rows.Count, "A").End(xlUp).Row))) For i = 1 To colCount - 1 For j = 1 To colCount - i If Cells(j + 1, 1).Value <> "" Then ' Check if the next cell is not empty before comparing values If Cells(j, 1).Value > Cells(j + 1, 1).Value Then temp = Cells(j, 1).Value Cells(j, 1).Value = Cells(j + 1, 1).Value Cells(j + 1, 1).Value = temp End If End If Next j Next iEnd Sub