Aporte - Ordenar Hojas de Un Libro de Excel
Posted: Sat Sep 01, 2012 5:17 am
Hola, el dia de hoy me solicitaron que de un libro de Excel, que genero en tiempo real y que tiene muchas hojas, que al final se vieran ordenadas ascendentemente, y buscando en el foro y en internet me encontre el siguiente codigo de visual basic, que usando el procesador de Macros de Excel ALT+F11, si funciona pero la idea es que nuestro sistema lo hiciera automaticamente:
que convertido a nuestro lenguaje quedo de la siguiente forma:
Espero les sirva de algo esta aportacion...
Code: Select all
Sub Sort_Active_Book()
Dim i As Integer
Dim j As Integer
Dim iAnswer As VbMsgBoxResult
'
' Prompt the user as which direction they wish to
' sort the worksheets.
'
iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
& "Clicking No will sort in Descending Order", _
vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
For i = 1 To Sheets.Count
For j = 1 To Sheets.Count - 1
'
' If the answer is Yes, then sort in ascending order.
'
If iAnswer = vbYes Then
If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
Sheets(j).Move After:=Sheets(j + 1)
End If
'
' If the answer is No, then sort in descending order.
'
ElseIf iAnswer = vbNo Then
If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
Sheets(j).Move After:=Sheets(j + 1)
End If
End If
Next j
Next i
End Sub
Code: Select all
IF msgyesno("Desea Ordenar las Hojas del Libro de Excel ?")
FOR i := 1 TO oExcel:Sheets:Count()
FOR j := 1 TO oExcel:Sheets:Count() - 1
IF UPPER( oExcel:WorkSheets( j ):Name ) > UPPER( oExcel:WorkSheets( j + 1):Name )
oSheet := oExcel:Sheets(oExcel:WorkSheets( j ):Name)
oExcel:Sheets( oExcel:WorkSheets( j + 1):Name ):Move( oSheet )
ENDIF
NEXT
NEXT