VBA for Excel 2007 different from Excel 2010?

zakynthos

Board Regular
Joined
Mar 28, 2011
Messages
169
Hi,

I've copied and tried the following code and it keeps looping around the
'mainloop' when I step through and check the 'local's window. It seems to run in Excel 2010 in the video I'm watching but I can't determine what's wrong when I run it in Excel 2007. One error I got was 'For without Next' - after 'For mainloop = 0 To UBound(names)' - but even though I input 'Next mainloop', it still doesn't run correctly.

Where is it going wrong?

__________________________________________________________

Sub sortnames()

Dim names(4) As String
Dim mainloop As Integer
Dim compareloop As Integer
Dim temp As String

names(0) = "bob"
names(1) = "BoB"
names(2) = "JOe"
names(3) = "Sally"
names(4) = "fRED"

For mainloop = 0 To UBound(names)

Next

For compareloop = mainloop To UBound(names)
If UCase(names(mainloop)) < UCase(names(compareloop)) Then

temp = names(mainloop)
names(mainloop) = names(compareloop)
names(compareloop) = temp

End If

Next compareloop


End Sub

________________________________________________


Thanks
 
Thanks for all your help on this! :)- your solution works perfectly and I've learned a bit more about loops and declaring variables
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,224,551
Messages
6,179,476
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top