Agregar al código formato de fecha

Pregunta para dante

Dante en esta macro que me hiciste se puede cambiar el formato de

Ene-2015 que tiene formato general x Ene-2015 formato personalizado mmm-yyyy

Porque como tengo un filtro y necesito filtrar x años gracias

Private Sub Worksheet_Change(ByVal Target As Range)'Por.Dante Amor    If Not Intersect(Target, Range("B:E")) Is Nothing Then        If Target.Count > 1 Then Exit Sub        If Target.Row < 4 Then Exit Sub        If Cells(Target.Row, "A") = "" Then            MsgBox "No hay fecha"            Exit Sub        End If        '        MES = Format(Cells(Target.Row, "A"), "mmm")        año = Year(Cells(Target.Row, "A"))        fecha = MES & "-" & año        Set b = Columns("G").Find(fecha, lookat:=xlWhole)        If Not b Is Nothing Then            f = b.Row        Else            f = Range("G" & Rows.Count).End(xlUp).Row + 1            If f < 4 Then f = 4        End If        Cells(f, "G") = "'" & fecha        c = Target.Column + 6        Cells(f, c) = Cells(f, c) + Target.Value        Cells(f, c).NumberFormat = "$* #,##0;[Red]$ * -#,##0"        Cells(f, "K") = Cells(f, "K") + Target.Value        Cells(f, "K").Interior.Color = 49407        Cells(f, "K").NumberFormat = "$* #,##0;[Red]$ * -#,##0"        With Range(Cells(f, "G"), Cells(f, "K")).Borders(xlEdgeBottom)            If UCase(MES) = "DIC" Then                .LineStyle = xlContinuous                .ColorIndex = 0                .TintAndShade = 0                .Weight = xlMedium            Else                .LineStyle = xlNone            End If        End With    End IfEnd Sub

Respuesta
1

Para que tenga formato de fecha, tiene que tener una fecha completa, lo que tu tienes solamente es mes y año, por eso lo puse en formato "Texto", para que sea una fecha hay que poner le día, entonces todas las fechas quedarán con 1-mes-año (el día uno), una vez que ya tiene una fecha, ya le puedes poner el formato que quieras, esto significa que puedes poner el formato "mmm-yyyy".

Te anexo la macro actualizada

Private Sub Worksheet_Change(ByVal Target As Range)
'Por.Dante Amor
    If Not Intersect(Target, Range("B:E")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Target.Row < 4 Then Exit Sub
        If Cells(Target.Row, "A") = "" Then
            MsgBox "No hay fecha"
            Exit Sub
        End If
        '
        'mes = Format(Cells(Target.Row, "A"), "mmm")
        'año = Year(Cells(Target.Row, "A"))
        'fecha = mes & "-" & año
        fecha = DateSerial(Year(Cells(Target.Row, "A")), Month(Cells(Target.Row, "A")), 1)
        Set b = Columns("G").Find(fecha, lookat:=xlWhole)
        If Not b Is Nothing Then
            f = b.Row
        Else
            f = Range("G" & Rows.Count).End(xlUp).Row + 1
            If f < 4 Then f = 4
        End If
        Cells(f, "G") = fecha
        Cells(f, "G").NumberFormat = "mmm-yyyy"
        c = Target.Column + 6
        Cells(f, c) = Cells(f, c) + Target.Value
        Cells(f, c).NumberFormat = "$* #,##0;[Red]$ * -#,##0"
        Cells(f, "K") = Cells(f, "K") + Target.Value
        Cells(f, "K").Interior.Color = 49407
        With Range(Cells(f, "G"), Cells(f, "K")).Borders(xlEdgeBottom)
            If Month(Cells(Target.Row, "A")) = 12 Then
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            Else
                .LineStyle = xlNone
            End If
        End With
    End If
End Sub

En esta parte de la macro le estoy diciendo que cambie el formato de la celda

Cells(f, "G").NumberFormat = "mmm-yyyy"

Si no te pone el formato, cambia el formato de toda la columna en formato de celdas, Personalizada y pon para toda la columna "mmm/aaaa", solamente lo tienes que hacer la primera vez.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas