- 27 Mar 2017 às 12:42
#21784
Bom dia!
Tenho uma rotina que está demorando mais de duas horas para atualizar uma planilha.
Resolvi fazer a atualização usando arrays. Inicialmente transfiro o range de toda planilha Tabela pra um array.
Ao tentar colocar uma fórmula dentro do array Atb1(cont_pr,6), retorna um erro em tempo de execução dizendo que o objeto é obrigatório. Não consegui descobri um jeito de colocar a fórmula dentro do array. Anexei o arquivo Teste.xlsb com três planilhas, sendo: Tabela: planilha de trabalho, Protheus: Planilha com os novos dados e Temp: Planilha onde será armazenados os dados atualizados. Abaixo o código que estou usando. Agradeceria se alguém pudesse encontrar uma maneira de colocar a fórmula no array.
Sub atualiza()
Application.StatusBar = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim nf_pr, nf_tb As String
Dim cnt_pr, cnt_pr1 As Integer
Dim LastCol_tb, LastCol_pr, LastRow_plan_tb, LastRow_tab_tb, LastRow_plan_pr, LastRow_tab_pr As Long
Dim Atb(), Atb1(), Apr() As Variant
Dim encontrado As Boolean
Set pr = Sheets("Protheus")
Set tb = Sheets("Tabela")
Set tmp = Sheets("Temp")
LastRow_plan_tb = tb.Range("A1").SpecialCells(xlCellTypeLastCell).Row
LastRow_tab_tb = tb.Cells(tb.Rows.Count, "A").End(xlUp).Row - 1
LastRow_plan_pr = pr.Range("A1").SpecialCells(xlCellTypeLastCell).Row
LastRow_tab_pr = pr.Cells(pr.Rows.Count, "A").End(xlUp).Row - 1
LastCol_tb = tb.UsedRange.Columns.Count
LastCol_pr = pr.UsedRange.Columns.Count
ReDim Atb(1 To LastCol_tb, 1 To LastRow_tab_tb)
Atb = tb.Range("A3:L" & LastRow_tab_tb).Value 'coloca todos os dados da planilha Tabela na memória
ReDim Atb1(1 To LastRow_tab_tb + LastRow_tab_tb, 1 To LastCol_tb)
ReDim Apr(1 To LastCol_tb, 1 To LastRow_tab_pr)
Apr = pr.Range("A3:J" & LastRow_tab_pr).Value 'coloca todos os dados da planilha Protheus na memória
For cnt_pr = 1 To UBound(Apr)
Application.StatusBar = "Pesquisando linha: " & " -> " & cnt_pr & " de " & UBound(Apr)
nf_pr = Apr(cnt_pr, 1) 'coloca numero da NF na variavel NF
encontrado = False
cnt_pr1 = 1
Do While cnt_pr1 <= UBound(Atb)
nf_tb = Atb(cnt_pr1, 7)
If nf_pr = nf_tb Then
Atb1(cnt_pr, 1) = Apr(cnt_pr, 10) ' Status
Atb1(cnt_pr, 2) = Apr(cnt_pr, 4) 'comprador
Atb1(cnt_pr, 3) = Atb(cnt_pr1, 3) 'representante
Atb1(cnt_pr, 4) = Atb(cnt_pr1, 4) 'UF de Origem
Atb1(cnt_pr, 5) = Apr(cnt_pr, 6) 'UF de Entrega
Atb1(cnt_pr, 6).FormulaArray = "{=IF([@STATUS]=""Em Aberto"",TODAY()-[@Emissão],"""")}"
Atb1(cnt_pr, 7) = Apr(cnt_pr, 1) 'número da NF
Atb1(cnt_pr, 8) = Apr(cnt_pr, 3) 'data de emissão
Atb1(cnt_pr, 9) = Apr(cnt_pr, 5) 'valor
Atb1(cnt_pr, 10) = Apr(cnt_pr, 7) 'valor do pagamento
Atb1(cnt_pr, 11) = Apr(cnt_pr, 9) 'data do pagamento
Atb1(cnt_pr, 12) = Apr(cnt_pr, 8) 'saldo
encontrado = True
Exit Do
End If
cnt_pr1 = cnt_pr1 + 1
Loop 'cnt_pr1
If Not encontrado Then
Atb1(cnt_pr, 1) = Apr(cnt_pr, 10) ' Status
Atb1(cnt_pr, 2) = Apr(cnt_pr, 4) 'comprador
Atb1(cnt_pr, 3) = "" 'representante
Atb1(cnt_pr, 4) = "" 'UF de Origem
Atb1(cnt_pr, 5) = Apr(cnt_pr, 6) 'UF de Entrega
Atb1(cnt_pr, 6).FormulaArray = "{=IF([@STATUS]=""Em Aberto"",TODAY()-[@Emissão],"""")}"
Atb1(cnt_pr, 7) = Apr(cnt_pr, 1) 'número da NF
Atb1(cnt_pr, 8) = Apr(cnt_pr, 3) 'data de emissão
Atb1(cnt_pr, 9) = Apr(cnt_pr, 5) 'valor
Atb1(cnt_pr, 10) = Apr(cnt_pr, 7) 'valor do pagamento
Atb1(cnt_pr, 11) = Apr(cnt_pr, 9) 'data do pagamento
Atb1(cnt_pr, 12) = Apr(cnt_pr, 8) 'saldo
End If
Next cnt_pr
On Error Resume Next
tmp.Range(Cells(3, 1), Cells(UBound(Atb1), LastCol_tb)) = Atb1
Application.StatusBar = False
End Sub
Tenho uma rotina que está demorando mais de duas horas para atualizar uma planilha.
Resolvi fazer a atualização usando arrays. Inicialmente transfiro o range de toda planilha Tabela pra um array.
Ao tentar colocar uma fórmula dentro do array Atb1(cont_pr,6), retorna um erro em tempo de execução dizendo que o objeto é obrigatório. Não consegui descobri um jeito de colocar a fórmula dentro do array. Anexei o arquivo Teste.xlsb com três planilhas, sendo: Tabela: planilha de trabalho, Protheus: Planilha com os novos dados e Temp: Planilha onde será armazenados os dados atualizados. Abaixo o código que estou usando. Agradeceria se alguém pudesse encontrar uma maneira de colocar a fórmula no array.
Sub atualiza()
Application.StatusBar = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim nf_pr, nf_tb As String
Dim cnt_pr, cnt_pr1 As Integer
Dim LastCol_tb, LastCol_pr, LastRow_plan_tb, LastRow_tab_tb, LastRow_plan_pr, LastRow_tab_pr As Long
Dim Atb(), Atb1(), Apr() As Variant
Dim encontrado As Boolean
Set pr = Sheets("Protheus")
Set tb = Sheets("Tabela")
Set tmp = Sheets("Temp")
LastRow_plan_tb = tb.Range("A1").SpecialCells(xlCellTypeLastCell).Row
LastRow_tab_tb = tb.Cells(tb.Rows.Count, "A").End(xlUp).Row - 1
LastRow_plan_pr = pr.Range("A1").SpecialCells(xlCellTypeLastCell).Row
LastRow_tab_pr = pr.Cells(pr.Rows.Count, "A").End(xlUp).Row - 1
LastCol_tb = tb.UsedRange.Columns.Count
LastCol_pr = pr.UsedRange.Columns.Count
ReDim Atb(1 To LastCol_tb, 1 To LastRow_tab_tb)
Atb = tb.Range("A3:L" & LastRow_tab_tb).Value 'coloca todos os dados da planilha Tabela na memória
ReDim Atb1(1 To LastRow_tab_tb + LastRow_tab_tb, 1 To LastCol_tb)
ReDim Apr(1 To LastCol_tb, 1 To LastRow_tab_pr)
Apr = pr.Range("A3:J" & LastRow_tab_pr).Value 'coloca todos os dados da planilha Protheus na memória
For cnt_pr = 1 To UBound(Apr)
Application.StatusBar = "Pesquisando linha: " & " -> " & cnt_pr & " de " & UBound(Apr)
nf_pr = Apr(cnt_pr, 1) 'coloca numero da NF na variavel NF
encontrado = False
cnt_pr1 = 1
Do While cnt_pr1 <= UBound(Atb)
nf_tb = Atb(cnt_pr1, 7)
If nf_pr = nf_tb Then
Atb1(cnt_pr, 1) = Apr(cnt_pr, 10) ' Status
Atb1(cnt_pr, 2) = Apr(cnt_pr, 4) 'comprador
Atb1(cnt_pr, 3) = Atb(cnt_pr1, 3) 'representante
Atb1(cnt_pr, 4) = Atb(cnt_pr1, 4) 'UF de Origem
Atb1(cnt_pr, 5) = Apr(cnt_pr, 6) 'UF de Entrega
Atb1(cnt_pr, 6).FormulaArray = "{=IF([@STATUS]=""Em Aberto"",TODAY()-[@Emissão],"""")}"
Atb1(cnt_pr, 7) = Apr(cnt_pr, 1) 'número da NF
Atb1(cnt_pr, 8) = Apr(cnt_pr, 3) 'data de emissão
Atb1(cnt_pr, 9) = Apr(cnt_pr, 5) 'valor
Atb1(cnt_pr, 10) = Apr(cnt_pr, 7) 'valor do pagamento
Atb1(cnt_pr, 11) = Apr(cnt_pr, 9) 'data do pagamento
Atb1(cnt_pr, 12) = Apr(cnt_pr, 8) 'saldo
encontrado = True
Exit Do
End If
cnt_pr1 = cnt_pr1 + 1
Loop 'cnt_pr1
If Not encontrado Then
Atb1(cnt_pr, 1) = Apr(cnt_pr, 10) ' Status
Atb1(cnt_pr, 2) = Apr(cnt_pr, 4) 'comprador
Atb1(cnt_pr, 3) = "" 'representante
Atb1(cnt_pr, 4) = "" 'UF de Origem
Atb1(cnt_pr, 5) = Apr(cnt_pr, 6) 'UF de Entrega
Atb1(cnt_pr, 6).FormulaArray = "{=IF([@STATUS]=""Em Aberto"",TODAY()-[@Emissão],"""")}"
Atb1(cnt_pr, 7) = Apr(cnt_pr, 1) 'número da NF
Atb1(cnt_pr, 8) = Apr(cnt_pr, 3) 'data de emissão
Atb1(cnt_pr, 9) = Apr(cnt_pr, 5) 'valor
Atb1(cnt_pr, 10) = Apr(cnt_pr, 7) 'valor do pagamento
Atb1(cnt_pr, 11) = Apr(cnt_pr, 9) 'data do pagamento
Atb1(cnt_pr, 12) = Apr(cnt_pr, 8) 'saldo
End If
Next cnt_pr
On Error Resume Next
tmp.Range(Cells(3, 1), Cells(UBound(Atb1), LastCol_tb)) = Atb1
Application.StatusBar = False
End Sub
Você não está autorizado a ver ou baixar esse anexo.