jalrs
Active Member
- Joined
- Apr 6, 2022
- Messages
- 300
- Office Version
- 365
- Platform
- Windows
Good morning guys,
If my macro doesn't find the filtered value, it is copying all the data which is wrong. How to prevent this? It should either just resume next i value for next filter value or just delete the template lines, leaving row 1 with the headers.
My loop code is as follows:
Any help is greatly appreciated.
Thanks!
If my macro doesn't find the filtered value, it is copying all the data which is wrong. How to prevent this? It should either just resume next i value for next filter value or just delete the template lines, leaving row 1 with the headers.
My loop code is as follows:
VBA Code:
For i = 2 To lr2
valorfiltro = Cells(i, 1).Value
Workbooks.Open Filename:=ThisWorkbook.Path & "\Controlo e Difusão\Templates\ST_TEMPLATE_" & Cells(i, 1).Value & ".xlsx"
Set wb2 = Workbooks("ST_TEMPLATE_" & valorfiltro & ".xlsx")
Set ws3 = wb2.Worksheets("Pendentes")
ws3.Activate
lr3 = ws3.Cells(Rows.Count, "A").End(xlUp).Row + 1
ws1.Activate
With ws1.Range("A5:AV" & lr1)
.AutoFilter 46, valorfiltro
.AutoFilter 47, "Em tratamento"
With ws1
.Range("A6:AV" & lr1).Copy
ws3.Cells(2, 1).PasteSpecial Paste:=xlPasteValues
.Range("BH6:BH" & lr1).Copy
ws3.Cells(2, 49).PasteSpecial Paste:=xlPasteValues
End With
Application.CutCopyMode = False
.AutoFilter
End With
lr3 = ws3.Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row + 1
ws3.Range("A" & lr3 & ":A1001").EntireRow.Delete
wb2.Activate ' don't think this part of the loop matters
ws3.Activate 'same as above
wb2.RefreshAll 'same as above
Range("A2").Select 'same as above
ws3.Protect Password:="blabla", _ 'same as above
DrawingObjects:=False, _ 'same as above
Contents:=True, _ 'same as above
Scenarios:=False, _ 'same as above
UserInterfaceOnly:=True, _ 'same as above
AllowFormattingCells:=True, _ 'same as above
AllowFormattingColumns:=True, _ 'same as above
AllowFormattingRows:=True, _ 'same as above
AllowInsertingColumns:=False, _ 'same as above
AllowInsertingRows:=False, _ 'same as above
AllowInsertingHyperlinks:=False, _ 'same as above
AllowDeletingColumns:=False, _ 'same as above
AllowDeletingRows:=False, _ 'same as above
AllowSorting:=True, _ 'same as above
AllowFiltering:=True, _ 'same as above
AllowUsingPivotTables:=False 'same as above
mypath = ThisWorkbook.Path & "\Controlo e Difusão\Partilhas e Regularizações\" 'same as above
wb1.Activate 'same as above
ws2.Activate 'same as above
docname = Cells(i, 5).Value 'same as above
wb2.Activate 'same as above
ws3.Activate 'same as above
ActiveWorkbook.SaveAs Filename:=mypath & docname & ".xlsx", FileFormat:=xlOpenXMLWorkbook 'same as above
ActiveWorkbook.Close 'until here 'same as above
Next i
Any help is greatly appreciated.
Thanks!