1

I know using Select is bad.

I'm trying to select cells from only the first row when multiple results are returned after autofiltering. I've used xlCellTypeVisible to copy all of the visible rows, but I'm unsure of how I could make this select cells in the C and D columns in the first filtered row. I would be taking values from the second autofilter before the activecellOffset lines.

For i = 2 To k
    Sheets("Database").Select
    ActiveSheet.Range("$A$1:$C" & k).AutoFilter Field:=2, Criteria1:= _
        Worksheets("SoPhiH").Range("A" & i).Value, Operator:=xlAnd
    Sheets("Database").Select
    ActiveSheet.Range("$A$1:$R" & k).AutoFilter Field:=5, Criteria1:= _
        Worksheets("SoPhiH").Range("C" & i).Value, Operator:=xlAnd
    Sheets("Database").Select
    If Application.WorksheetFunction.Subtotal(3, Range("A2:A" & k)) Then ' If the cell is blank copy and paste, else do nothing
    Else
    ActiveSheet.Range("$A$1:$C" & k).AutoFilter Field:=2
    ActiveSheet.Range("$A$1:$C" & k).AutoFilter Field:=5

    Sheets("SoPhiH").Select
    ActiveSheet.Range("A" & i).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Database").Select
    Range("B" & Rows.Count).End(xlUp).Offset(1).Select
    ActiveSheet.Paste
    UWI = ActiveCell.Value
    ActiveSheet.Range("$A$1:$C" & k).AutoFilter Field:=2, Criteria1:= _
        UWI, Operator:=xlAnd

    ActiveCell.Offset(0, 3) = Worksheets("SoPhiH").Range("C" & i)
    ActiveCell.Offset(0, 17) = Worksheets("SoPhiH").Range("B" & i)
    End If

    Next i

1 Answer 1

1

Many of the same methods used to manipulate a conventional range can be used on the range created by specialcells.

with ActiveSheet.Range("$A$1:$C" & k)
    .AutoFilter Field:=2, Criteria1:=UWI
    with .offset(1, 0)
        .specialcells(xlcelltypevisible).offset(0, 2).resize(1, 2).select
    end with
end with

If you are dealing with more than the first row, the Areas property my have to be used.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.