Page 1 of 1

Aporte - Ordenar Hojas de Un Libro de Excel

Posted: Sat Sep 01, 2012 5:17 am
by derpipu
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:

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
 
que convertido a nuestro lenguaje quedo de la siguiente forma:

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

 
Espero les sirva de algo esta aportacion...

Re: Aporte - Ordenar Hojas de Un Libro de Excel

Posted: Sat Sep 01, 2012 3:01 pm
by Patricio Avalos Aguirre
Gracias Derpipu

Re: Aporte - Ordenar Hojas de Un Libro de Excel

Posted: Sat Sep 01, 2012 3:40 pm
by Armando
Luis Fernando:

Un tequila por eso, Gracias

Saludos

Re: Aporte - Ordenar Hojas de Un Libro de Excel

Posted: Sun Sep 02, 2012 2:29 am
by wmormar
Armando wrote:Luis Fernando:

Un tequila por eso, Gracias

Saludos
Mejor la botella, jjejjejjejjejje

Re: Aporte - Ordenar Hojas de Un Libro de Excel

Posted: Sun Sep 02, 2012 2:29 am
by wmormar
Armando wrote:Luis Fernando:

Un tequila por eso, Gracias

Saludos
Mejor la botella, jjejjejjejjejje