Notifications
Clear all

Macro lenta (possivelmente lógica ruim)

6 Posts
2 Usuários
0 Reactions
1,066 Visualizações
(@renanm)
Posts: 5
Active Member
Topic starter
 

Boa tarde,

Gostaria de saber se alguém pode me ajudar na lógica de uma macro, pois a que programei está muito lenta para executá-la. Segue abaixo a macro.

Private Sub lançar()

Sheets("EXTRATO ANUAL").Activate

On Error Resume Next
Dim rec As String
Dim dep As String
Dim cc As String
Dim mes As String
Dim valor As Double
Dim nf As Double
Dim data As Date
Dim forn As String
Dim pag As Double
Dim cxb As String
Dim prod As String
Dim med As String
Dim qtd As Double
Dim bch As String

rec = ComboBox1
dep = ComboBox2
cc = ComboBox3
mes = ComboBox4
valor = TextBox1
nf = TextBox2
data = TextBox3
forn = TextBox4
pag = TextBox5
cxb = ComboBox5
prod = TextBox6
med = ComboBox6
qtd = TextBox7
bch = TextBox8



On Error Resume Next

    If valor > 0 And rec = "Despesa" Then
    valor = "-" & valor

        End If


     If pag <> "" And rec = "Despesa" Then
    pag = "-" & pag

    
    End If

Sheets("EXTRATO ANUAL").Activate

Range("C2").Select

Do

    If ActiveCell.Value <> Empty Then
        ActiveCell.Offset(1, 0).Activate
    End If
        
Loop Until ActiveCell.Value = Empty


ActiveCell = dep
ActiveCell.Offset(0, 1).Activate
ActiveCell = cc
ActiveCell.Offset(0, 1).Activate
ActiveCell = mes
ActiveCell.Offset(0, 1).Activate
ActiveCell = valor
ActiveCell.Offset(0, 1).Activate
ActiveCell = nf
ActiveCell.Offset(0, 1).Activate
ActiveCell = data
ActiveCell.Offset(0, 1).Activate
ActiveCell = forn
ActiveCell.Offset(0, 1).Activate
ActiveCell = pag
ActiveCell.Offset(0, 1).Activate
ActiveCell = cxb
ActiveCell.Offset(0, 1).Activate
ActiveCell = prod
ActiveCell.Offset(0, 1).Activate
ActiveCell = med
ActiveCell.Offset(0, 1).Activate
ActiveCell = qtd
ActiveCell.Offset(0, 1).Activate
ActiveCell = bch
ActiveCell.Offset(0, 1).Activate

End Sub

Utilizo um form com combobox e listview, que ao preenche-los, eles atualizam ou inserem dados em uma planilha. Contudo, essa macro demora de 20 a 30seg para executar. Alguém poderia me ajudar?

Obrigado

 
Postado : 21/02/2017 9:52 am
Fernando Fernandes
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Tenta assim:

Private Sub lançar()
On Error Resume Next
Dim rec             As String, dep      As String
Dim cc              As String, mes      As String
Dim valor           As Double, nf       As Double
Dim data            As Date, forn       As String
Dim pag             As Double, cxb      As String
Dim prod            As String, med      As String
Dim qtd             As Double, bch      As String

Dim ULTIMA_LINHA    As Long
Dim PROXIMA_LINHA   As Long
    
    Sheets("EXTRATO ANUAL").Activate
    rec = ComboBox1
    dep = ComboBox2
    cc = ComboBox3
    mes = ComboBox4
    valor = TextBox1
    nf = TextBox2
    data = TextBox3
    forn = TextBox4
    pag = TextBox5
    cxb = ComboBox5
    prod = TextBox6
    med = ComboBox6
    qtd = TextBox7
    bch = TextBox8
    
    If valor > 0 And rec = "Despesa" Then valor = "-" & valor
    
    If pag <> "" And rec = "Despesa" Then pag = "-" & pag
    
    Sheets("EXTRATO ANUAL").Activate
    
    ULTIMA_LINHA = Range("C1048576").End(xlUp).Row
    PROXIMA_LINHA = ULTIMA_LINHA + 1
    
    Range("C" & PROXIMA_LINHA & ":O" & PROXIMA_LINHA).Value = Array(dep, cc, mes, valor, nf, data, forn, pag, cxb, prod, med, qtd, bch)

End Sub

Existem mil maneiras de preparar Neston. Invente a sua!
http://www.youtube.com/ExpressoExcel

 
Postado : 21/02/2017 10:01 am
(@renanm)
Posts: 5
Active Member
Topic starter
 

ok, tentarei. Darei o feedback logo logo.
Obrigado desde já...

 
Postado : 21/02/2017 6:16 pm
(@renanm)
Posts: 5
Active Member
Topic starter
 

O código está OK.. apenas tenho um problema: Eu possuo uma tabela na sheets extrato anual, e quando executo esse código, o mesmo me retorna após a ultima linha da tabela. O código não consegue enxergar que não há dados na tabela, apenas fora da mesma. Há alguma maneira de resolver isso, sem remover a tabela?

 
Postado : 23/02/2017 7:50 am
(@renanm)
Posts: 5
Active Member
Topic starter
 

Aproveitando, se alguém puder (desculpa a "folgadisse") me ajudar com a lógica desse código também:

Private Sub btnOK1_Click()

Application.ScreenUpdating = False

On Error Resume Next

    Dim x As Double

    x = TextBox1

    If TextBox1 > 0 And ComboBox1 = "Despesa" Then
    TextBox1 = "-" & x
    
    End If

    Dim p As Double
    On Error Resume Next
    p = TextBox5

    If TextBox5 > 0 And ComboBox1 = "Despesa" Then
    TextBox5 = "-" & p
    
    End If

    Dim plan As Worksheet
    Dim i As Integer
    Dim dep As String
    Dim cc As String
    Dim mes As String
    Dim valor As Double
    Dim nf As Double
    Dim data As Date
    Dim forn As String
    Dim pag As Double
    Dim cxb As String
    Dim prod As String
    Dim med As String
    Dim qtd As Double
    Dim bch As String
    Dim proc As String
    
    Set plan = Sheets("EXTRATO ANUAL")

    linha = 3
    dep = ComboBox2
    cc = ComboBox3
    mes = ComboBox4
    valor = TextBox1
    nf = TextBox2
    data = TextBox3
    forn = TextBox4
    pag = TextBox5
    cxb = ComboBox5
    prod = TextBox6
    med = ComboBox6
    qtd = TextBox7
    bch = TextBox8
    proc = Label30

    plan.Select

    linha = plan.Range("X:X").Find(proc).Row

    With plan
    
    .Cells(linha, 3) = dep
    .Cells(linha, 4) = cc
    .Cells(linha, 5) = mes
    .Cells(linha, 6) = valor
    .Cells(linha, 7) = nf
    .Cells(linha, 8) = data
    .Cells(linha, 9) = forn
    .Cells(linha, 10) = pag
    .Cells(linha, 11) = cxb
    .Cells(linha, 12) = prod
    .Cells(linha, 13) = med
    .Cells(linha, 14) = qtd
    .Cells(linha, 15) = bch

    End With

Application.ScreenUpdating = True

Call atualizar

End Sub

Após eu clicar em uma linha na minha listview e ela preencher minhas textbox e combobox com dos dados, esse código vai procurar na minha planilha essa linha que cliquei (por números sequenciais) e edita-los conforme eu alterar as textbox e combobox. Contudo, a lógica desse código está dando lentidão na macro. Alguém poderia me ajudar a melhorá-la?

Obrigado!

 
Postado : 23/02/2017 8:18 am
(@renanm)
Posts: 5
Active Member
Topic starter
 

Alguém?

 
Postado : 24/02/2017 6:00 am