I am having an issue where a particular Column has blanks in the middle of it for certain rows. This is leading the code:
Range(FieldName.Offset(1), FieldName.End(xlDown)).Select
To not select all of the cells since it is stopping at the blank cells and filling in XYZ for the cells right before the blanks.
I know that xlup will remedy this issue, however, if the last cell of the field is blank then it will not change that cell and go to the next populated cell. I am not sure on how to modify my code so that it utilizes xlup and avoids if the bottom cells are blank in the column. I do have a column named "ABC" that will always have all of its rows populated that I can maybe ping off of in order to call it out as the last row of the filtered data, but I am not sure how to do this.
My Code
Sub SelectDown()
Dim FieldName As Range
Dim rng As Range, res As Variant, lrow As Long
Set rng = ActiveSheet.AutoFilter.Range.Rows(1)
res = Application.Match("Errors", rng, 0)
'Finds the Specific Error'
rng.AutoFilter Field:=res, Criteria1:="*-SHOULD BE XYZ*"
'Only Shows rows that have something that matches the filter criteria
lrow = ActiveSheet.Cells(Rows.Count, res).End(xlUp).Row + 1
If ActiveSheet.Range(Cells(1, res), Cells(lrow, res)).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
Set FieldName = Range("A1:BZ1").Find("COLUMN NAME")
'If field isnt present shows message
If FieldName Is Nothing Then
MsgBox "Field Name was not found."
End If
'Changes the Selection to XYZ if there is a change present
Range(FieldName.Offset(1), FieldName.End(xlDown)).Select
Selection.FormulaR1C1 = "XYZ"
'Changes the Color of the fields changed to Yellow
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
End With
Else
End If
End Sub
Range(FieldName.Offset(1), FieldName.End(xlDown)).Select
at theElse
section ofIf FieldName Is Nothing Then
, to make sureFind
method for findingFieldName
was successful. – Shai Rado