Thursday, August 30, 2012

VBA - Sort with dynamic range

Dim col As Long
Dim fil As Long
Dim rango As String

fil = Worksheets(hoja).Cells(Worksheets(hoja).Rows.Count, "A").End(xlUp).Row
col = Worksheets(hoja).Range("A1").End(xlToRight).Column

rango = Range(Cells(1, 1), Cells(fil, col)).Address
Worksheets(hoja).Range(rango).Sort Key1:=Worksheets(hoja).Columns("A"), Order1:=xlAscending, Header:=xlYes

Wednesday, August 29, 2012

Create chart and paste as image Vba

image

image

Sub grafico_dinamico()
    ' Crear gráfico
    Range("A10:C16").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Sheets("Ind_01").Range("A10:C16")
    ActiveChart.ChartType = xl3DColumnClustered
    ActiveChart.Parent.Name = "Ind_01"


    ' Pegar como imagen
    ActiveSheet.ChartObjects("Ind_01").Activate
    ActiveChart.ChartArea.Copy
    Range("A50").Select
    ActiveSheet.Pictures.Paste.Select
End Sub

Last cell vba (row – column)

row = Worksheets("Ind_01").Cells(Worksheets("Ind_01").Rows.Count, "A").End(xlUp).Row

col =  Worksheets("Val_org").Range("A1").End(xlToRight).Column

Custom traffic light icon set (xl3TrafficLights1)

image

Previamente se define qué valores se tendrán dependiendo de las condiciones deseadas:

=IF(L2<=H2,2,IF(L2<=G2,1, 0))

Sub Color_semaforos()
    Dim cfIconSet As IconSetCondition

    ' Rangos a trabajar
    Range("F5:F25").Select

    Selection.FormatConditions.AddIconSetCondition
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1)
        .ReverseOrder = False
        .ShowIconOnly = True
        .IconSet = ActiveWorkbook.IconSets(xl3TrafficLights1)
    End With
    With Selection.FormatConditions(1).IconCriteria(2)
        .Type = xlConditionValueNumber
        .Value = 1
        .Operator = 7
    End With
    With Selection.FormatConditions(1).IconCriteria(3)
        .Type = xlConditionValueNumber
        .Value = 2
        .Operator = 7
    End With

End Sub