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