+ Reply to Thread
Results 1 to 7 of 7

Filldown a VLookup formula

Hybrid View

  1. #1
    Registered User
    Join Date
    10-25-2019
    Location
    NSW< Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Filldown a VLookup formula

    Sub testfill()
    
    
    Selection.Offset(0, -3).Select
        Selection.End(xlDown).Select
            Selection.End(xlToRight).Select
                Selection.Offset(, 1).Select
                    Selection.End(xlUp).Offset(1).Select
    
    Dim myrange As Range
     Set myrange = Range("New!A:C")
     
    Dim LastRow As Integer
    LastRow = WorksheetFunction.CountA(Range("A:A"))
    
    
     activecell.FormulaR1C1 = _
            "=IF(VLOOKUP(RC[-3],New!C[-3]:C[-1],3,FALSE)=0,""-"",VLOOKUP(RC[-3],New!C[-3]:C[-1],3,FALSE))"
            
    
    End Sub
    Hello. The above code works as required. What I cannot work out is how to reference the range I require and Filldown the formula.

    The lastrow dimension finds the last row with data. The range I want to fill down will be from the active cell down to the row specified by lastrow, in a single column that the active cell is in.

    The active cell will be different each time the code runs and the last row will vary each time as well.

    How to I write the Filldown code to reference the range I want?

    Many thanks

  2. #2
    Registered User
    Join Date
    10-25-2019
    Location
    NSW< Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Filldown a VLookup formula

    Sub testfill()
    
    Dim first As Range
    Set first = activecell
    
    Selection.Offset(0, -3).Select
        Selection.End(xlDown).Select
            Selection.End(xlToRight).Select
                Selection.Offset(, 1).Select
                
    Dim last As Range
            Set last = activecell
    
    Selection.End(xlUp).Offset(1).Select
    
    Dim myrange As Range
            Set myrange = Range("New!A:C")
    
    
     activecell.FormulaR1C1 = _
    "=IF(VLOOKUP(RC[-3],New!C[-3]:C[-1],3,FALSE)=0,""-"",VLOOKUP(RC[-3],New!C[-3]:C[-1],3,FALSE))"
            
    With activecell
    .AutoFill Destination:=Range(first, last)
    End With
    
    
    
    End Sub
    I got it to work about 1 minute after I posted.

  3. #3
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: Filldown a VLookup formula

    Quote Originally Posted by redhill View Post
    I got it to work about 1 minute after I posted.
    It looks like the code will be very simple if you attach a sample workbook and explain what do you want.
    ❖ Please mark your thread is SOLVED if there has been offered a solution that works fine for you.

    ❖ If you like solutions provided by anyone, feel free to add reputation by clicking on ✶ Add Reputation bottom left of their posts.

  4. #4
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: Filldown a VLookup formula

    Try this
    Sub testfill()
    Dim Rng As Range, LastRow As Integer
    LastRow = WorksheetFunction.CountA(Range("A:A"))
    Set Rng = Selection.Offset(0, -3).End(xlDown).End(xlToRight).Offset(, 1).End(xlUp).Offset(1)
    Set Rng = Rng.Resize(LastRow - Rng.Row + 1)
    Rng.FormulaR1C1 = _
            "=IF(VLOOKUP(RC[-3],New!C[-3]:C[-1],3,FALSE)=0,""-"",VLOOKUP(RC[-3],New!C[-3]:C[-1],3,FALSE))"
    'Rng.Select 'If you want to select the range that have been set formula
    End Sub

  5. #5
    Registered User
    Join Date
    10-25-2019
    Location
    NSW< Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Filldown a VLookup formula

    Now it works I get an N/A error where there is no value for the VLookup formula, the formula isn't replacing N/A with "-". I tried an IFERROR and VLOOKUP nested but it comes back with a Type mismatch error. Any suggestions?

  6. #6
    Registered User
    Join Date
    10-25-2019
    Location
    NSW< Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Filldown a VLookup formula

    Thank you huuthang_bd that is much tidier than my code. Still getting the N/A error though.

  7. #7
    Registered User
    Join Date
    10-25-2019
    Location
    NSW< Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Filldown a VLookup formula

    Got rid of the N/A error. I didn't have the IFERROR formula correct. Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Dynamic Formula filldown in VBA
    By ImranBhatti in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-08-2017, 02:52 PM
  2. [SOLVED] Simple formula FillDown in VBA
    By Fowzee1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2015, 01:11 AM
  3. [SOLVED] VBA to Filldown Every Other Column with Formula
    By msawyer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-29-2013, 02:29 PM
  4. [SOLVED] VBA Vlookup Filldown/Autofill in Alternating Columns
    By msawyer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-29-2013, 01:25 PM
  5. macro for filldown a formula
    By judasdac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2009, 01:11 PM
  6. Filldown Formula
    By Carlie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-19-2006, 06:59 PM
  7. Macro Filldown only with Formula
    By Ben Noall in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2006, 10:35 AM

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