Private Sub TESTE()
xApplicationOptimize True
Set FileDialog = Application.FileDialog(msoFileDialogFolderPicker)
FileDialog.Title = "Selecione a pasta: FINAL"
If FileDialog.Show = -1 Then xPathName = FileDialog.SelectedItems(1) Else Exit Sub
If Right(xPathName, 1) <> "\" Then xPathName = xPathName + "\"
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Selecione o arquivo: BASE_J"
.InitialFileName = "*.xlsx"
If .Show() Then xFileName = .SelectedItems(1) Else Exit Sub
End With
Workbooks.Open Filename:=xFileName
Set xOldWB = ActiveWorkbook
Application.WindowState = xlMinimized
xL = 2
Do While Not IsEmpty(xOldWB.Sheets(1).Cells(xL, 1))
NOVAMENTE:
xFalse = xOldWB.Sheets(1).Cells(xL, 2)
xTrue = xOldWB.Sheets(1).Cells(xL, 3)
xLine = xOldWB.Sheets(1).Cells(xL, 4)
If xTrue = "" Then
xL = xL + 1
GoTo PULAR
End If
If xOldWB.Sheets(1).Cells(xL, 1) = xOldWB.Sheets(1).Cells(xL - 1, 1) And xTrue <> "" Then GoTo CONTINUA
xFileName = "FINAL_" & Format(xOldWB.Sheets(1).Cells(xL, 1), "YYYY_MM_DD") & ".xlsx"
On Error GoTo PULAR
Workbooks.Open Filename:=xPathName & xFileName
Set xNewWB = ActiveWorkbook
Application.WindowState = xlMinimized
CONTINUA:
With ActiveSheet
.Rows(1).Insert
.AutoFilte1rMode = False
With Range("A1", Range("O" & Rows.Count).End(xlUp))
.AutoFilter Field:=2, Criteria1:=xFalse
.AutoFilter Field:=4, Criteria1:=xLine
Range("B1", Range("B" & Rows.Count).End(xlUp)) = xTrue
End With
.AutoFilterMode = False
With Range("B1", Range("B" & Rows.Count).End(xlUp))
.AutoFilter 1, "N/A"
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False
.Rows(1).Delete
End With
xL = xL + 1
If xOldWB.Sheets(1).Cells(xL, 1) = xOldWB.Sheets(1).Cells(xL - 1, 1) And xTrue <> "" Then GoTo NOVAMENTE
xNewWB.Save
xNewWB.Close
PULAR:
Loop
xOldWB.Close
xApplicationOptimize False
End Sub