Notifications
Clear all

Otimizando Código

6 Posts
3 Usuários
0 Reactions
2,401 Visualizações
(@brunoafs)
Posts: 195
Reputable Member
Topic starter
 

Bom dia galera,

Criei um código aos trancos e barrancos,
Quando rodo ela por partes ele funciona tranquilo.

Quando rodo ele todo o Excel fica 'travado' e o código não termina.

Alguém me ajuda em alguma parte que fica mais rápido.

Private Sub ConfBase()

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

On Error Resume Next

' Base Anterior vs. Base Atual

Dim a, b, c As Long
Dim MesAnterior, MesAtual As Date

MesAnterior = CDate("31/10/2019")
MesAtual = CDate("30/11/2019")

c = Planilha3.Range("B1048576").End(xlUp).Row

    For a = 2 To 100000
            
            If Planilha2.Cells(a, 1) = "" Then Exit For
                If Planilha2.Cells(a, 2) = MesAnterior And Planilha2.Cells(a, 10) <> "" Then
                    
                b = 2
                Do While b <= Planilha2.Range("A1048576").End(xlUp).Row
                    
                    If Planilha2.Cells(b, 2) = MesAtual Then
                        If Planilha2.Cells(b, 10) = Planilha2.Cells(a, 10) Then
                            If Planilha2.Cells(b, 12) <> Planilha2.Cells(a, 12) Or Planilha2.Cells(b, 17) <> Planilha2.Cells(a, 17) Then
                            
                            Planilha3.Cells(c, 2) = Planilha2.Cells(a, 10) 'id Posição
                            Planilha3.Cells(c, 3) = MesAnterior            'Data Base Anterior
                            Planilha3.Cells(c, 4) = Planilha2.Cells(a, 12) 'Cargo
                            Planilha3.Cells(c, 5) = Planilha2.Cells(a, 17) 'Diretoria
                            
                            Planilha3.Cells(c, 6) = MesAtual               'Data Base Anterior
                            Planilha3.Cells(c, 7) = Planilha2.Cells(b, 12) 'Cargo
                            Planilha3.Cells(c, 8) = Planilha2.Cells(b, 17) 'Diretoria
                            
                            c = c + 1
                            
                            Exit Do
                            
                            Else
                            
                            Exit Do
                            
                            End If
                                               
                        Else
                        
                        b = b + 1
                        
                        End If
                                                
                    Else
                    
                    b = b + 1
                        
                    End If
                Loop
                End If
    Next a
    
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub
 
Postado : 26/12/2019 9:24 am
(@klarc28)
Posts: 0
New Member
 
Private Sub ConfBase()

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

On Error Resume Next

' Base Anterior vs. Base Atual

Dim a, b, c As Long
Dim MesAnterior, MesAtual As Date

MesAnterior = CDate("31/10/2019")
MesAtual = CDate("30/11/2019")

c = Planilha3.Range("B1048576").End(xlUp).Row

    For a = 2 To Planilha2.usedrange.rows.count
           
            If Planilha2.Cells(a, 1) = "" Then Exit For
                If Planilha2.Cells(a, 2) = MesAnterior And Planilha2.Cells(a, 10) <> "" Then
                   
                b = 2
                Do While b <= Planilha2.Range("A1048576").End(xlUp).Row
                   
                    If Planilha2.Cells(b, 2) = MesAtual Then
                        If Planilha2.Cells(b, 10) = Planilha2.Cells(a, 10) Then
                            If Planilha2.Cells(b, 12) <> Planilha2.Cells(a, 12) Or Planilha2.Cells(b, 17) <> Planilha2.Cells(a, 17) Then
                           
                            Planilha3.Cells(c, 2) = Planilha2.Cells(a, 10) 'id Posição
                            Planilha3.Cells(c, 3) = MesAnterior            'Data Base Anterior
                            Planilha3.Cells(c, 4) = Planilha2.Cells(a, 12) 'Cargo
                            Planilha3.Cells(c, 5) = Planilha2.Cells(a, 17) 'Diretoria
                           
                            Planilha3.Cells(c, 6) = MesAtual               'Data Base Anterior
                            Planilha3.Cells(c, 7) = Planilha2.Cells(b, 12) 'Cargo
                            Planilha3.Cells(c, 8) = Planilha2.Cells(b, 17) 'Diretoria
                           
                            c = c + 1
                           
                            Exit Do
                           
                            Else
                           
                            Exit Do
                           
                            End If
                                               
                        Else
                       
                        b = b + 1
                       
                        End If
                                               
                    Else
                   
                    b = b + 1
                       
                    End If
                Loop
                End If
    Next a
   
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub
 
Postado : 26/12/2019 9:49 am
(@brunoafs)
Posts: 195
Reputable Member
Topic starter
 

klarc28,

Mesmo com a alteração do "Planilha2.usedrange.rows.count" o código processa por completo em +- 3 minutos.

Não sei se é normal devido a rotina ou se tem alguma forma de otimizar ainda mais.

 
Postado : 26/12/2019 11:56 am
(@ewshaka)
Posts: 0
New Member
 

Boa tarde BrUnOaFs.

Seria mais fácil se você colocasse uma planilha exemplo para visualizarmos a estrutura e dar uma resposta mais assertiva.

Em todo caso, não daria para substituir esse For pelo Do Until?

Att.

 
Postado : 26/12/2019 12:47 pm
(@brunoafs)
Posts: 195
Reputable Member
Topic starter
 

ewshaka,

Em anexo segue a planilha base.

Em resumo:

Na guia TB_BASE eu preciso colocar Mês a Mês uma base de colaboradores.
No exemplo tem apenas os meses de Out/19 e Nov/19

Eu preciso rodar uma rotina mensalmente onde o Loop é:

Procurar todas as "posições" que fica na coluna "J" da Data Anterior (No caso Outubro) na Data do mês seguinte (No caso Novembro).

SE o cargo (Coluna L) ou a Diretoria (Coluna Q) tiver alguma alteração ela informa na guia "Conferência"

https://1drv.ms/u/s!AjgPdMTXNagYgtwAE-9v_aL3hTWStw?e=sT6f19

 
Postado : 26/12/2019 1:27 pm
(@brunoafs)
Posts: 195
Reputable Member
Topic starter
 

ewshaka,

Quando uma "posição" só consta em uma base (No caso, apenas em Outubro),
O código coloca valores errados.

Exemplo:

A posição "80603938" só consta em Outubro, e ele trás valores da posição de linha 4892
Não sei se como a lógica se comporta quando não acha a posição.

 
Postado : 27/12/2019 11:40 am