vlookup result in formula not value

Costa_Mukhar

New Member
Joined
Jun 6, 2015
Messages
20
Hello experts, I am using the below code for a vlookup, it is working perfectly except that in the first cell it is giving the result in value not formula

Code:
Dim cellvalue As String
    Dim lookup_range As range
    Dim coupon As Single
    
    cellvalue = Worksheets("Cost report").range("B5")
    Set lookup_range = Worksheets("BA").range("D:H")
    
    coupon = Application.WorksheetFunction.VLookup(cellvalue, lookup_range, 5, False)
    range("C5:C" & range("B" & Rows.Count).End(xlUp).Row) = coupon

in cell C5, the result is in value, so when the macro applies the formula on the selected range, it is copying the same value in cell C5 to the entire range. how to keep the formula in the first cell, so it can apply it on the entire range then turn the entire column into values. I feel that there is something missing in the code, but I am not able to figure it out!!

Thanks in advnace
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Costa_Mukhar

New Member
Joined
Jun 6, 2015
Messages
20
YES....I got it :) it feels amazing when you figure it by yourself ;)

Code:
Dim cellvalue As String
    Dim lookup_range As range
    Dim coupon As Single
    
    cellvalue = Worksheets("Cost report").range("B5:B" & Rows.Count).End(xlUp).Row
    Set lookup_range = Worksheets("BA").range("D:H")
    
    'coupon = Application.WorksheetFunction.VLookup(cellvalue, lookup_range, 5, False)
    'range("C5:C" & range("B" & Rows.Count).End(xlUp).Row) = coupon
    
    With Worksheets("cost report").range("C5:C" & range("B" & Rows.Count).End(xlUp).Row)
        .FormulaR1C1 = "=vlookup(RC[-1],'BA'!C4:C8,5,False)"
        .FormulaR1C1 = .Value
        
    End With
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,101
Messages
5,835,389
Members
430,354
Latest member
Novice125

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top