+ Reply to Thread
Results 1 to 7 of 7

Find first blank cell after Auto Filter

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Find first blank cell after Auto Filter

    Greetings, Gurus.

    Suppose I am using auto filter on a range to filter down to only the blank cells in column C, and then I want to enter a formula into all those blank cells. How would I determine the range of the first blank cell? Here's what I have so far:

    Sub Macro1()
        Range("C1").AutoFilter Field:=3, Criteria1:="="
        Range("C477").Select 'Needs to be first visible cell after the autofilter is applied.
        ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(RC[-2],'[PLine Listing.xlsx]COM_ REPLEN1677'!C1:C2,2,FALSE)"
        Range("C477").Copy 'Same as above
        Range("C477:C" & Cells(Rows.Count, "A").End(xlUp).Row).Select
        ActiveSheet.Paste
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        Range("A1").Select
        ActiveSheet.AutoFilterMode = False
    End Sub
    Thanks in advance for any help you can offer.
    Last edited by hutch@edge.net; 05-05-2010 at 11:40 AM.

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Find first blank cell after Auto Filter

    Hi, Could you use this, The "Specialcells" function will Give you the addresses of the Blank Cells, and then next line will fill them with th Formula.
    Sub MG04May18
    Dim Rng As Range
    Set Rng = Range(Range("C1"), Range("C" & Rows.count).End(xlUp))
    Set Rng = Rng.SpecialCells(xlCellTypeBlanks)
    MsgBox Rng.address
    Rng.FormulaR1C1 = "=VLOOKUP(RC[-2],'[PLine Listing.xlsx]COM_ REPLEN1677'!C1:C2,2,FALSE)"
    
    End Sub
    Regards Mick

  3. #3
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Find first blank cell after Auto Filter

    Thanks for the reply, Mick. When I tried your code, I got the following error:
    Run-time error '1004': No cells were found. The debug highlighted the following line:

    Set Rng = Rng.SpecialCells(xlCellTypeBlanks)

    Yes, there are blank cells in the range.
    Last edited by hutch@edge.net; 05-04-2010 at 11:48 AM.

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Find first blank cell after Auto Filter

    Hi, I know this sounds rather obvious but, I can only suggest you try the code on a new sheet.
    Place data in column "C", then Delete some of the values.
    See what the code returns.
    Regards Mick

  5. #5
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Find first blank cell after Auto Filter

    Sorry so slow to repond, Mick. I had to leave the office early yesterday.

    I figured out the problem. The reason your script isn't finding any empty cells is because of the line:
    Set Rng = Range(Range("C1"), Range("C" & Rows.count).End(xlUp))
    It is telling it to only look in cells that already contain data. How would I chnage that line to look at all cells in column C, down to the last row in column A. I tried

    Set Rng = Range(Range("C1"), Range("C" & Rows.Count, "A").End(xlUp))
    but I get a "Method 'Range' of object '_Global' failed" error.

  6. #6
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Find first blank cell after Auto Filter

    Hi, Try :-
    Set rng = Range(Range("A1"), Range("A" & Rows.count).End(xlUp)).Offset(, 2)
    Regards Mick

  7. #7
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Find first blank cell after Auto Filter

    Perfect! Here's the final code as I use it:
    Sub Lookup_Pline()
        Dim Rng As Range
        Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp)).Offset(, 2)
        Set Rng = Rng.SpecialCells(xlCellTypeBlanks)
        Rng.FormulaR1C1 = "=VLOOKUP(RC[-2],'[PLine Listing.xlsx]COM_ REPLEN1677'!C1:C2,2,FALSE)"
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        Range("C1").AutoFilter Field:=3, Criteria1:="=#N/A", _
            Operator:=xlAnd
        Range("A1").Select
    End Sub
    Thanks, Mick. Have a good one.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1