I have code that filters a column, then adds values to the visible cells of an adjacent column.
The code determines the row number of the first visible cell excluding the column label. It uses this row number in the formula it pastes into that cell. Copies the cell.
It uses the filtered column to reach the last used/visible cell of the filtered column - moves over one column and selects all visible cells up to the first visible cell again excluding the column lable.
This codes keeps copying my formula the row AFTER the last used row in my column and I can't figure out why?
I suspect the reason could be I am inserting a dummy row under the column labels so that the visiblecells.area(2) is always the first visible cell under the column labels. I don't think I had this error before I started inserting a row. Please help - code is below
Sub ICB_AIR_categories()
'Insert the row that ensures category macro runs correctly
Rows("2:2").Select
Selection.Insert Shift:=xlDown
Range("C2").Select
ActiveCell.FormulaR1C1 = "MACRO"
'Fill Receipt with the lookup
With Range("B1").CurrentRegion
.AutoFilter Field:=3, Criteria1:="AIR"
.AutoFilter Field:=1, Criteria1:=""
End With
If Range("B1").End(xlDown).Value = "" Then
Range("B1").CurrentRegion.AutoFilter
Else
NROW = Range("B1").CurrentRegion.SpecialCells(xlCellTypeVisible).Areas(2).Row
Range("A" & NROW).Formula = "=VLOOKUP(F" & NROW & ",[OwnerMap.xls]AIR!AIRDATA,4,FALSE)"
Range("A" & NROW).Copy
Range("B1").End(xlDown).Select
ActiveCell.Offset(0, -1).Select
Range(Selection, Selection.End(xlUp)).Offset(1, 0).SpecialCells(xlCellTypeVisible).Select
ActiveSheet.Paste
Range("B1").CurrentRegion.AutoFilter
End If
The code determines the row number of the first visible cell excluding the column label. It uses this row number in the formula it pastes into that cell. Copies the cell.
It uses the filtered column to reach the last used/visible cell of the filtered column - moves over one column and selects all visible cells up to the first visible cell again excluding the column lable.
This codes keeps copying my formula the row AFTER the last used row in my column and I can't figure out why?
I suspect the reason could be I am inserting a dummy row under the column labels so that the visiblecells.area(2) is always the first visible cell under the column labels. I don't think I had this error before I started inserting a row. Please help - code is below
Sub ICB_AIR_categories()
'Insert the row that ensures category macro runs correctly
Rows("2:2").Select
Selection.Insert Shift:=xlDown
Range("C2").Select
ActiveCell.FormulaR1C1 = "MACRO"
'Fill Receipt with the lookup
With Range("B1").CurrentRegion
.AutoFilter Field:=3, Criteria1:="AIR"
.AutoFilter Field:=1, Criteria1:=""
End With
If Range("B1").End(xlDown).Value = "" Then
Range("B1").CurrentRegion.AutoFilter
Else
NROW = Range("B1").CurrentRegion.SpecialCells(xlCellTypeVisible).Areas(2).Row
Range("A" & NROW).Formula = "=VLOOKUP(F" & NROW & ",[OwnerMap.xls]AIR!AIRDATA,4,FALSE)"
Range("A" & NROW).Copy
Range("B1").End(xlDown).Select
ActiveCell.Offset(0, -1).Select
Range(Selection, Selection.End(xlUp)).Offset(1, 0).SpecialCells(xlCellTypeVisible).Select
ActiveSheet.Paste
Range("B1").CurrentRegion.AutoFilter
End If