PDA

View Full Version : [SOLVED:] Filter and name the rows when available



Hudson
11-26-2016, 05:14 AM
Hi all ,

I have this code that does a simple job . only when criteria is met if not . it is going astray .

For example : below code filter column (K) " DA,DB,DZ" and put a a comment in adjacent column ("S") " Deduction". so far so good . But code is changing first cell of column ("S") even though documnt type (DA,DB,DZ) misses.



Sub filter_deduction()

Range("K1:K" & Range("K" & Rows.Count).End(3).Row).AutoFilter 1, Criteria1:=Array("DA", "DB", "DZ"), Operator:=xlFilterValues

Range("S2:S" & Range("K" & Rows.Count).End(3).Row).SpecialCells(12).Formula = "Deductions"


ActiveSheet.AutoFilterMode = False
End Sub

mana
11-26-2016, 06:21 AM
on error resume next

Hudson
11-26-2016, 06:45 AM
Hi mana.


i already tried that . unfortunately i still comment in first cell .

p45cal
11-26-2016, 12:32 PM
Sub filter_deduction()
ActiveSheet.AutoFilterMode = False
lr = Range("K" & Rows.Count).End(3).Row
Range("K1:K" & lr).AutoFilter 1, Criteria1:=Array("DA", "DB", "DZ"), Operator:=xlFilterValues
On Error Resume Next
Range("S2:S" & lr).SpecialCells(12).Value = "Deductions"
On Error GoTo 0
ActiveSheet.AutoFilterMode = False
End Sub
Important changes highlighted below in red:

Sub filter_deduction()
ActiveSheet.AutoFilterMode = False
lr = Range("K" & Rows.Count).End(3).Row
Range("K1:K" & lr).AutoFilter 1, Criteria1:=Array("DA", "DB", "DZ"), Operator:=xlFilterValues
On Error Resume Next
Range("S2:S" & lr).SpecialCells(12).Value = "Deductions"
On Error GoTo 0
ActiveSheet.AutoFilterMode = False
End Sub

Hudson
11-27-2016, 12:37 AM
Perfect . i got it now. thanks mate ... with your help i was able to do beginner level VBA ..i am really indebted to you .