Notifications
Clear all

Ajuda para Agilizar o código.

2 Posts
2 Usuários
0 Reactions
903 Visualizações
(@pvitor)
Posts: 11
Active Member
Topic starter
 

Bom dia! Estou com uma dúvida a fim de deixar o código abaixo mais rápido.

Qual estrutura utilizar para fugir do .Select na maioria dos condicionais?

Obrigado.

Public Sub Formatação()

    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    
'
' Realiza a formatação das planilhas
'
    Cells.Select
    With Selection.Font
        .Name = "Calibri"
        .Size = 9
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With


'
' Formatação Condicional da Coluna Estado
'

    ' Ligado
    Columns("D:D").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""Ligado"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorAccent1
        .TintAndShade = -0.499984740745262
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Desligado
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""Desligado"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16721442
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Em Movimento
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""EmMovimento"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16744448
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Parado
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""Parado"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16776961
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Ocioso
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""Ocioso"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -6279056
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Desconectado
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""Desconectado"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.349986266670736
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    
'
' Formatação Condicional da Coluna Vel. (Km/h)
'
    Columns("E:E").Select
    ' Velocidade entre 1 e 19
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=1", Formula2:="=19"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16744448
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Velocidade entre 20 e 39
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=20", Formula2:="=39"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16721442
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Velocidade entre 40 e 59
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=40", Formula2:="=59"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16094238
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Velocidade entre 60 e 79
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=60", Formula2:="=79"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -13355370
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Velocidade entre 80 e 99
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=80", Formula2:="=99"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16776961
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Velocidade acima de 100
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
        Formula1:="=100"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -6584783
        .TintAndShade = 0
    End With
    ' Velocidade igual a 0
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.499984740745262
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    
'
' Formatação da Coluna Dist. (Km)
'

    Columns("F:F").NumberFormat = "#,##0.00"
    
'
' Formatação da Coluna Latitude e Longitude
'
    
    Columns("H:I").NumberFormat = "#,##0.00000"
    
'
' Formatação da Coluna Volt. Ext (V)
'
    
    Columns("J:J").NumberFormat = "#,##0.00"
    
'
' Formatação Condicional da Coluna IOS
'
        
    Columns("N:N").Select
    ' Ativo
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""Ativo"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16744448
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    ' Inativo
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""Inativo"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16776961
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False

' Oculta algumas colunas

'    Columns("O:O").EntireColumn.Hidden = True
    Columns("P:P").EntireColumn.Hidden = True
    Columns("Q:Q").EntireColumn.Hidden = True
    Columns("S:S").EntireColumn.Hidden = True
    
' Negrito na primeira linha

    Rows("1:1").Font.Bold = True
    
    
' Enquadra as colunas

    Cells.EntireColumn.AutoFit
    
' Congela o cabeçalho

    Rows("2:2").Select
    ActiveWindow.FreezePanes = True
        
        
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
'    ActiveSheet.Calculate

End Sub
 
Postado : 07/10/2016 7:18 am
(@mprudencio)
Posts: 2749
Famed Member
 

Olhei seu codigo e aparentemente ele so faz formatação codicional,

Não é mais simples deixar um arquivo como base pronto e so alimentar com dados (mesmo que tenha que fazer um arquivo/planilha) para cada mes

Marcelo Prudencio
Microsoft Excel Brasil no Facebook

"Começar já é a metade do caminho."
Autor Desconhecido

Simplifica que simples fica.
Nicole Tomazella.

"O Simples é Sempre Melhor Que o Complicado"
Jorge Paulo Lemann.

 
Postado : 07/10/2016 10:11 am