I'm attempting to use Excel to execute what is ostensibly a straightforward bubble sort algorithm. I've attempted to run bubble sort several times, but each time I receive the message "Compile error sub or function is not declared." I'm making use of the code my professor provided. Thank you.

```Sub BubbleSort()

'   Sorts an array using bubble sort algorithm

For i = 1 To 20
For j = 1 To 20 - i
If Cells(j, 1) > Cells(j + 1, 1) Then
Temp = Cells(j, 1)
Sleep 10
Cells(j, 1) = Cells(j + 1, 1)
Cells(j + 1, 1) = Temp

Application.Wait (Now + TimeValue("0:00:001"))

End If

Next
Next

End Sub
```

I have tried using a vba syntax checker. But quite frankly, I have no experience with vb and do not know where to start.

Jan 30, 2023 in Others 291 views

## 1 answer to this question.

Bubble Sort a Single-Column Range

A Quick Fix

```Option Explicit

' Sorts the range A1:A20 using the bubble sort algorithm
Sub BubbleSort()

Const FIRST_ROW As Long = 1
Const LAST_ROW As Long = 20

Dim ws As Worksheet: Set ws = ActiveSheet ' improve

Dim Temp, i As Long, j As Long

For i = FIRST_ROW To LAST_ROW - 1
For j = i + 1 To LAST_ROW
If ws.Cells(i, "A").Value > ws.Cells(j, "A").Value Then
Temp = ws.Cells(i, "A").Value
ws.Cells(i, "A").Value = ws.Cells(j, "A").Value
ws.Cells(j, "A").Value = Temp
End If
Next j
Next i

MsgBox "Column range sorted.", vbInformation

End Sub```
• 63,720 points

+1 vote

## I have worked in IT field for 9+ years, but after 2016, due to my family issues, i cant continue working, now, I am searching job, how can i get job after long gap

Hi, @Saravanan, Let me tell you one thing ...READ MORE

## How to get the URL of the current tab in Google Chrome?

Its so simple.... If you want to ...READ MORE

+1 vote

## I am not able to see the chat box on webinar-session. i can see only ask question pane

Today (8/aug/2020) I have joined one webinar, ...READ MORE

## ERROR: You need elevated Administrator privileges in order to run this script.

Hi@akhtar, I guess you are trying to run ...READ MORE

## Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

## How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

## Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE