Sub INSERIR_PRODUTO_2025()
On Error GoTo TratarErro
Application.ScreenUpdating = False
Dim UltimaLinhaENTRADA_PROD As Long
Dim UltimaLinhaPREÇO_VENDA As Long
Dim UltimaLinhaInserir As Long
Dim CopiarIntervalo As Range
Dim wsinserir As Worksheet, wsENTRADA_PROD As Worksheet, wsPREÇO_VENDA As Worksheet
' Definir planilhas
Set wsinserir = Sheets("INSERIR_PRODUTO_2025")
Set wsENTRADA_PROD = Sheets("ENTRADA_PROD")
Set wsPREÇO_VENDA = Sheets("PREÇO_VENDA")
' Determinar última linha nas planilhas destino
UltimaLinhaENTRADA_PROD = wsENTRADA_PROD.Cells(wsENTRADA_PROD.Rows.Count, 3).End(xlUp).Row
UltimaLinhaPREÇO_VENDA = wsPREÇO_VENDA.Cells(wsPREÇO_VENDA.Rows.Count, 3).End(xlUp).Row
' Determinar última linha preenchida na planilha de inserção
UltimaLinhaInserir = wsinserir.Cells(wsinserir.Rows.Count, 2).End(xlUp).Row
If UltimaLinhaInserir < 4 Then
MsgBox "Nenhum dado para inserir.", vbExclamation
Exit Sub
End If
' Copiar os dados DATA / CÓDIGO
Set CopiarIntervalo = wsinserir.Range("B4:C" & UltimaLinhaInserir)
wsENTRADA_PROD.Range("B" & UltimaLinhaENTRADA_PROD + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
' Outras operações de cópia (sem alterações significativas)
' ...
' Copiar Grupo / Conta / Observação / Entrada Prod.
Set CopiarIntervalo = wsinserir.Range("E4:I" & UltimaLinhaInserir)
wsENTRADA_PROD.Range("D" & UltimaLinhaENTRADA_PROD + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
'Custo / Valor / Desc. / Total > Valor Contábil
Set CopiarIntervalo = wsinserir.Range("J4:M" & UltimaLinhaInserir)
wsENTRADA_PROD.Range("I" & UltimaLinhaENTRADA_PROD + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
' Aplicar a formatação ao intervalo copiado
wsENTRADA_PROD.Range("I" & UltimaLinhaENTRADA_PROD + 1 & ":M" & UltimaLinhaENTRADA_PROD + CopiarIntervalo.Rows.Count).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
' Chamar o cálculo do saldo
Call InserirSaldo(UltimaLinhaENTRADA_PROD, CopiarIntervalo.Rows.Count)
' Copiar Cod. Fornecdor
Set CopiarIntervalo = wsinserir.Range("D4:D" & UltimaLinhaInserir)
wsENTRADA_PROD.Range("O" & UltimaLinhaENTRADA_PROD + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
' Copiar os dados FORNECEDOR
Set CopiarIntervalo = wsinserir.Range("N4:N" & UltimaLinhaInserir)
wsENTRADA_PROD.Range("P" & UltimaLinhaENTRADA_PROD + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
' Copiar os dados CONCATENAR
Set CopiarIntervalo = wsinserir.Range("AI4:AK" & UltimaLinhaInserir)
wsENTRADA_PROD.Range("Q" & UltimaLinhaENTRADA_PROD + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
'PREÇO DE VENDA
' Copiar os dados DATA / CÓDIGO
Set CopiarIntervalo = wsinserir.Range("B4:C" & UltimaLinhaInserir)
wsPREÇO_VENDA.Range("B" & UltimaLinhaPREÇO_VENDA + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
' Copiar > Grupo / Produto
Set CopiarIntervalo = wsinserir.Range("E4:F" & UltimaLinhaInserir)
wsPREÇO_VENDA.Range("D" & UltimaLinhaPREÇO_VENDA + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
' Copiar > Valor Dia Contábil
Set CopiarIntervalo = wsinserir.Range("M4:M" & UltimaLinhaInserir)
wsPREÇO_VENDA.Range("F" & UltimaLinhaPREÇO_VENDA + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
' Aplicar a formatação ao intervalo copiado
wsPREÇO_VENDA.Range("F" & UltimaLinhaPREÇO_VENDA + 1 & ":G" & UltimaLinhaPREÇO_VENDA + CopiarIntervalo.Rows.Count).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
' Chamar o cálculo do saldo
Call InserirSaldo(UltimaLinhaPREÇO_VENDA, CopiarIntervalo.Rows.Count)
'Copiar > Estoque Entrada do Produto
Set CopiarIntervalo = wsinserir.Range("P4:P" & UltimaLinhaInserir)
wsPREÇO_VENDA.Range("H" & UltimaLinhaPREÇO_VENDA + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
' Copiar > Custo Produto Contábil
Set CopiarIntervalo = wsinserir.Range("Q4:AG" & UltimaLinhaInserir)
wsPREÇO_VENDA.Range("I" & UltimaLinhaPREÇO_VENDA + 1).Resize(CopiarIntervalo.Rows.Count, CopiarIntervalo.Columns.Count).Value = CopiarIntervalo.Value
' Aplicar a formatação ao intervalo copiado
wsPREÇO_VENDA.Range("I" & UltimaLinhaPREÇO_VENDA + 1 & UltimaLinhaPREÇO_VENDA + CopiarIntervalo.Rows.Count).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
MsgBox "Dados inseridos e saldo calculado com sucesso!", vbInformation
Application.ScreenUpdating = True
Exit Sub
TratarErro:
MsgBox "Erro ao executar a macro: " & Err.Description, vbCritical
Application.ScreenUpdating = True
End Sub
Sub InserirSaldo(UltimaLinhaENTRADA_PROD As Long, CopiarLinhas As Long)
Dim SaldoFormula As String
Dim i As Long
Dim wsENTRADA_PROD As Worksheet
' Definir a planilha de destino
Set wsENTRADA_PROD = Sheets("ENTRADA_PROD")
' Inserir SALDO na coluna M
For i = 0 To CopiarLinhas - 1
If UltimaLinhaENTRADA_PROD + i = 1 Then
' Primeira linha com saldo inicial
SaldoFormula = "=L" & (UltimaLinhaENTRADA_PROD + 1)
Else
' Linhas subsequentes: soma acumulada do saldo
SaldoFormula = "=M" & (UltimaLinhaENTRADA_PROD + i) & "+L" & (UltimaLinhaENTRADA_PROD + 1 + i)
End If
' Aplicar a fórmula na célula da coluna M
wsENTRADA_PROD.Range("M" & (UltimaLinhaENTRADA_PROD + 1 + i)).Formula = SaldoFormula
Next i
End Sub