|
Bubble Sort
This macro will perform a bubble sort in excel. You use it simply by selecting one column to sort and then running the macro.
This is basically just like a regular sort but just done using the bubble sort method of comparing two contiguous cells and then switching cell position between those two cells so that those two are arranged in a local ascending or descending order. This is then repeated until the entire sequence is sorted in the correct order. This type of sort takes more time than a quick sort or than many other types of sorts; however, it still works and is widely taught.
Note: If you want to sort in descending order, replace the "<" with a ">" sign in the line that reads "If Val(sortingArray(j, 1)) < Val(sortingArray(i, 1)) Then."
Where to install the macro: Module
Bubble Sort Macro in Excel
Sub bubble_sort()
Dim sortingArray As Variant, i As Long, j As Long, temp As Variant
sortingArray = Selection.Value
For i = 1 To (UBound(sortingArray, 1) - 1)
For j = i To UBound(sortingArray, 1)
If Val(sortingArray(j, 1)) < Val(sortingArray(i, 1)) Then
temp = sortingArray(i, 1)
sortingArray(i, 1) = sortingArray(j, 1)
sortingArray(j, 1) = temp
End If
Next j
Next i
Selection.Value = sortingArray
End Sub
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007 Go to Tools > Macros > Visual Basic Editor
For Excel 2007 Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.
Similar Helpful Excel Resources
Hi Everyone,
Does anyone know how I can select (to edit) a bubble data point that is "behind" another larger bubble? My bubbles aren't filled; they are just circles. But I still can't seem to select a smaller bubble sitting behind / inside a larger bubble.
Cheers,
Jay
i need help any example post on bubble sort function not a sub that can sort three numbers and display it in any way via the three sorting cells or in the function cell using only single array dimension. also the function should use as double for all numbers to sort.
here the sample question from our work shop:
function using a form of the bubble sort algorithm which we
explored in a previous workshop. I suggest you start with the Matlab code that you wrote
previously and consider how you can convert it into VBA. You may wish to simplify the algorithm
since you only have three numbers to sort. If you set your outer loop to cycle through each pair of
numbers twice this will ensure that you have correctly sorted the three numbers.
plz need help asap.
I'm using a bubble sort routine that is dependent upon the value in another cell. It works great. However, I'm putting together a UserForm with a dropdown combobox, single selection.
At the moment, I'm taking the values from the arrary sort and placing them into separate cells and then reading that range in through rowsource. The arrary values are not all the same. Some could be one value while another is 12.
What would be a good way to make a dynamic range, that can be read into a UserForm, that only displays the values from the sort without a lot of extra blank rows?
Thanks for your time and expertise,
Maverick
I've been using bubble charts quite a bit but haven't figured out how to move
a smaller bubble that is hidden to the front of a larger bubble. Any help
would be appreciated.
I've seen pie charts inserted into bubble charts, but I'd like to just modify the bubble color in one series to incorporate a 4th continuous variable. So input data are four continuous variables, two for the axes, one for bubble size, and another for shading. Any ideas welcome!
I have created an bubble chart in excel 2007. I would like to make the bubbles 50% transparent using vba. I know how to do it manually and I have tried recording the macro while doing it. Strange enough, the macro is empty. Since it actually is possible to change the transparency of the bubbles, there should be a way to achieve the same result in vba. Does anyone know how? Thanks for your help in advance!
Bryan
ps. I have already tried:
Sub a()
Workbooks("book").Worksheets("sheet").ChartObjects("chart").Activate
ActiveChart.SeriesCollection(2).Format.Fill.Transparency = 0.5
End Sub
It runs, but nothing happens.
Hi,
I'm doing up an organisational chart, and would like to have a "bubble" appear next to a position within the chart, when the cursor hovers over the position's (text)box in the chart. Or, when the position box is clicked on. I'm thinking it's much like adding a 'comments box' to the text box.
Is this possible?
Tks.
Hi,
This is my first thread in this forum and I could some with building a bubble chart that fits my needs. I did not find a solution using the search function. I am working on survey data and would like to review age groups vs income groups vs the number of respondents belonging into one these groups (bubble size). I have 7 age groups:
17-25, 26-35, ... 76+
and 15 income groups starting from "less than 3 Million", "3.0-3.9" ... "more than 40 Million".
I attached a sample data set to this post.
What I would like to do is to get the following categories into a bubble chart. I am well aware that using Excel the categories have to be in the following order:
x-axis values, y-axis values, z-values
On the x-axis, I would like to put the age groups with the income groups on the y-axis.
I dont know how to tell Excel that there can be different income groups belonging to one age group.
It would be great if someone could tell me a method to accomplish this.
Best Regards
P.S.: Would a scatter plot provide the better solution?
I've looked through the various thread on bubble chart but i am still having problems so would appreciate any help i could get.....
List of names......Age......height.....weight
A.....................17.........160........60
B.....................25.........180........70
C.....................18.........149........45
D.....................30.........149........45
+30 other rows
I managed to map the Age (x); height (y); weight (size of bubble) but i still can't add the list of names as lables to the bubbles........i saw a thread on this which mentioned adding a series but i can't seem to get it to work. Please help!
|
|