0% found this document useful (0 votes)
106 views

VBA - Bubble Sort. A Bubble Sort Is A Technique To Order - by Breakcorporate - Medium

The document describes how to perform a bubble sort algorithm to sort an array of integers in ascending order using VBA code. It includes an example array with 10 integers, and code that uses nested for loops to iterate through the array. The inner loop compares each integer to its adjacent larger integer, swapping the two if the larger is actually smaller. This process continues until the array is fully sorted from smallest to largest.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

VBA - Bubble Sort. A Bubble Sort Is A Technique To Order - by Breakcorporate - Medium

The document describes how to perform a bubble sort algorithm to sort an array of integers in ascending order using VBA code. It includes an example array with 10 integers, and code that uses nested for loops to iterate through the array. The inner loop compares each integer to its adjacent larger integer, swapping the two if the larger is actually smaller. This process continues until the array is fully sorted from smallest to largest.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

breakCorporate 6 Followers About Follow Upgrade

VBA — Bubble Sort


breakCorporate Jul 10, 2016 · 2 min read

A bubble sort is a technique to order items in an array. They can be set in


either ascending or descending order. It is possible to output items in an
array to Excel and then call the Sort method on that range. However, this is
not best practice.

We have the following array:

Dim sortArray(9) as Integer


sortArray(0) = 100
sortArray(1) = 20
sortArray(2) = 30
sortArray(3) = 70
sortArray(4) = 10
sortArray(5) = 40
sortArray(6) = 90
sortArray(7) = 80
sortArray(8) = 60
sortArray(9) = 50

For i = 0 to Ubound(sortArray)

For x = Ubound(sortArray) to i + 1 Step - 1

if sortArray(x) < sortArray(i) Then

holdInt = sortArray(x)
sortArray(x) = sortArray(i)
sortArray(i) = holdInt

end if

Next x

Next i

The code should sort the array in ascending order. The outer loop runs
through every item in the array from index 0 to the upper bound 9. The
inner loop runs from the upper bound 9 to i + 1. Step-1 indicates that the
iteration will be negative. So step-1 would go from 9 to 1.

For example:

// The first loop iteration

i = 0 in the first loop


sortArray(i) = 100

x = the int in inner loop which runs from 9 to 1


100 is checked against each item in those indices

If the number at index x is less than the number at index i


Then the item at x is switched with the item at i

The holdInt variable is used to store the value at x


Once we set the item at x equal to the item at i
The value at x is gone, we use holdInt to store that value
Now we set the item at i to the holdInt item

This process continues for each index in the sortArray


Once the first loop is complete the item at index 0 will be 10

The next loop i will be 1


The inner loop will run from 9 to 2

There is no need to run from 9 to 1 or 9 to 0


The item at 0 is already the lowest value
The item at 1 is the current index being evaluated
There is no need to check it against itself

Programming Vba Excel Corporate Innovation Automation

More from breakCorporate Follow

Writing about the shortcomings of Corporations, the breakdown in the US


education system, and why you shouldnt be afraid to take risks.

Jul 10, 2016

VBA — Add Items to an Array


Arrays in VBA are a little Knicky and require a diMerent procedure than
programming langauges like C or Python. In VBA you cannot append an
item to an array. Instead you must re-dimension the array to be one index
larger than the previous.

To initialize the array:

Dim arr() as String

To add an item to the array:

Redim preserve arr(0)


arr(0) = string1

Redim preserve arr(1)


arr(1) = string2

or

Redim preserve arr(Ubound(arr) + 1)


arr(Ubound(arr)) = string2

You cannot check the Ubound (upper bounds) of an array if it does not have
a dimension.

It is also important…

Read more · 1 min read

84 1

Jul 10, 2016

Automate Excel Monotony


Ah, end of the month again and I need to setup those fucking reports. Every
month it’s the same thing with one little change. Fuck it, it’s the same thing.
Input the data into this sheet, run that model, update a sheet. There it is.
One of 20 reports is done. Oh sorry, I don’t want you to think updating a
report was as quick and simple as that short sentence. No, this is a
painstaking task that requires double checking every piece before and after.
Now if that process fails in the model, well shit, that adds another
headache…

Read more · 2 min read

About Help Legal

You might also like