Macro Looping Problem

  • Thread starter Thread starter spudgun44
  • Start date Start date
S

spudgun44

Help please, I am new to macro and VBA and have a problem,

I neen a macro to run when a worksheet tab is clicked, the macro
peforma a sort on the rest of the tabs the problem is that i need it
to end on that tab and to sort, so when that tab is selected at the
end of the macro the macro runs again and goes into a loop.

Is that a way to run the macro once and stop it?

Thanks
 
Here is one way:

Right click on the sheet tab of the sheet that should sort the sheets.
Select "View Code" and paste in the following two functions:

Private Sub Worksheet_Activate()
Call SheetSortDOSLogic
End Sub

Public Function SheetSortDOSLogic()
Dim lngX As Integer
Dim lngY As Integer
Dim lngSheetCount As Long
lngSheetCount = ActiveWorkbook.Sheets.Count
For lngX = 1 To lngSheetCount
For lngY = lngX + 1 To lngSheetCount
If Sheets(lngX).Name > Sheets(lngY).Name Then
Sheets(lngY).Move Before:=Sheets(lngX)
End If
Next lngY
Next lngX
End Function

To make it run again, you must select a different tab and then reselect the
"sort" sheet tab.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top