martes, noviembre 18, 2014

Formatear celdas en Flexigrid


$(".flexme5").flexigrid({
                title: 'Overview by category', 
                url: 'PnLCalculations.asmx/GetRowData2',
                dataType: 'json',
                method: 'post',
                params: [{ name: 'uid', value: '<%= cSession.UserWeb %>' },
                         { name: 'entityId', value: '<%= cSession.EntityCode %>' },
                         { name: 'period', value: '<%= ddlPeriod.Text %>' },
                         { name: 'secondGroupOrder', value: 'Category' }
                         ],
                colModel: [
                { display: 'Folders Shortname', name: 'col01', width: 100, sortable: true, draggable: false, align: 'left' },
                { display: 'Category', name: 'col02', width: 100, sortable: true, draggable: false, align: 'left' },
                { display: 'Sum', name: 'col03', width: 100, sortable: true, draggable: false, align: 'right' },
                { display: 'Count', name: 'col04', width: 100, sortable: true, draggable: false, align: 'right' }
                ],
                sortname: "row01",
                sortorder: "asc",
                usepager: true,
                autoload: true,
                resizable: false,
                singleSelect: true,
                useRp: false,
                rp: 25,
                width: twidth,
                height: 505,
                preProcess: function(data) {
                    $.each(data.rows, function(i, row) {
                       if (parseInt(row.col03)<0) {
                          row.col03='

'+row.col03+'

'; } if (parseInt(row.col03)>0) { row.col03='

'+row.col03+'

'; } }); return data; }, });

Función para convertir un número de columna en su letra en Excel

Función VBA para convertir un número de columna en su nombre en letras, para usarlo en Excel.

Function numberToColumnName(ByVal colNumber As Integer) As String
   Dim dividend As Integer
   Dim columnName As String
   Dim module As Integer
   
   dividend = colNumber
   
   While (dividend > 0)
      module = (dividend - 1) Mod 26
      columnName = Chr(65 + module) + columnName
      
      dividend = (dividend - module) / 26
   Wend
   numberToColumnName = columnName
End Function

jueves, agosto 28, 2014

Abrir carpeta desde VBA

Función que abre una carpeta desde VBA. Llamando al shell de windows, lanzo la apertura de la carpeta.

Sub openFolder(folderName As String)
    retVal = Shell("explorer.exe " & folderName, vbNormalFocus)
End Sub


Comprobar si existe un archivo en VBA

Función para comprobar si existe o no un archivo. Como parámetro de entrada tengo la ruta del archivo y la función devuelve si existe o no.

Function fileExists(filename) As Boolean
   Dim fso, msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.fileExists(filename)) Then
      fileExists = True
      Exit Function
   Else
      fileExists = False
      Exit Function
   End If
End Function

viernes, agosto 22, 2014

Convertir fecha de formato mm/dd/yyyy a dd/mm/yyyy

Me pasaron un excel con fechas en formato inglés en modo texto. Así es cómo las he transformado en fecha en español. Soy consciente de que esta no es la forma mas elegante de hacer esto, pero tenía prisa...

Sub flipDate()
   i = 2
   While (Range("M" & i) <> "")
    
     If Len(Range("M" & i)) = 9 Then
        mm = Left(Range("M" & i), 1)
        dd = Mid(Range("M" & i), 3, 2)
        yyyy = Right(Range("M" & i), 5)
        
     Else
        mm = Left(Range("M" & i), 2)
        dd = Mid(Range("M" & i), 4, 2)
        yyyy = Right(Range("M" & i), 4)
        
     End If
    
     Range("M" & i).Value = ""
     Range("M" & i) = Format(Date, "dd/mm/yyyy")
     Range("M" & i).Value = dd & "/" & mm & "/" & yyyy
     
     
     'Range("M" & i).Activate
     
     DoEvents
     i = i + 1
     'Debug.Print i
     
     'Application.ScreenUpdating = False
   Wend
     Application.ScreenUpdating = True
     MsgBox ("Done!")
End Sub