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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Shouldn't the loops be nested?

Code:
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)


    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
Next mainloop


End Sub
 
Upvote 0
Sorry, I didn't see the amendment you made before, but have just tried it and now get a 'Next without For' error when stepping through, so I guess nesting doesn't work!
 
Upvote 0
I just ran Peter's code; it is without flaw.
 
Upvote 0
Thanks, so how come I get an error message when i run the identical code - could I have a setting somewhere in options etc that is causing error messages?
 
Upvote 0
Correction,very sorry!!

I've tried copying and pasting this over mine and it now runs perfectly.

Many thanks for your advice
 
Upvote 0
One last question - if I want this to run on the names listed say, in Column A what do I need to add to the code to get it to do the sort - I've tried 'Columns("A:A").Select' but while that selects Column A, the names don't get sorted.

Thanks
 
Upvote 0
Maybe like this

Code:
Sub sortnames()

Dim names() As String
Dim mainloop As Integer
Dim compareloop As Integer
Dim temp As String
Dim LR As Long, i As Long

LR = Range("A" & Rows.Count).End(xlUp).Row
ReDim names(0 To LR - 1)
For i = LBound(names) To UBound(names)
    names(i) = Range("A" & i + 1).Value
Next i

For mainloop = 0 To UBound(names)


    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
Next mainloop
For i = LBound(names) To UBound(names)
    Range("A" & i + 1).Value = names(i)
Next i


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,545
Messages
6,179,432
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