VBA Lookup Returning Formula Instead of Value

Altonfree

New Member
Joined
Feb 3, 2011
Messages
8
Trying to complete a macro to pull data from one worksheet to update erroneous data on another. There are three columns to work with, and for the first two columns, the code works just fine. For the final column, the macro cycles through the first If statement with no trouble, but returns the vlookup formula, not the value, for the following two Else If statements. The format of all columns appears to be text, though I have formatted it within the macro as well to make sure. But with or without the formatting, I get the same result:

Code:
Range("K:K").NumberFormat = "@"
Range("K2").Select
 Do While Not IsEmpty(ActiveCell.Offset(0, 3))
    If ActiveCell.Offset(0, 3) = "A" Then
    ElseIf ActiveCell.Offset(0, 3) = "I" Then
        ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-10],DFLT,8,0)"
    ElseIf ActiveCell.Offset(0, 3) = "C" Then
        ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-10],DFLT,8,0)"
    End If
    ActiveCell.Offset(1, 0).Select
   Loop

Any assistance would be welcome.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
So, you just want to appear the value of the VLOOKUP and not the VLOOKUP formula itself?
Try something like this (note that I tried to improve the speed and efficiency of the code by getting rid of all the ActiveCell and Select statements - that slows your code down and it is not necessary to select the ranges to work with them):
Code:
    Dim r As Long

    Range("K:K").NumberFormat = "@"

    r = 2
    Do While Not IsEmpty(Cells(r, "N"))
        Select Case Cells(r, "N").Value
            Case "I", "C"
                Cells(r, "K").FormulaR1C1 = "=VLOOKUP(RC[-10],DFLT,8,0)"
                Cells(r, "K").Value = Cells(r, "K").Value
        End Select
        r = r + 1
   Loop
 
Upvote 0
Thanks for the response, and sorry for the late followup. I tried your code, but got the same result: the macro returned the formula instead of the value.
 
Upvote 0
It shouldn't.

Can you post a data example, so we can make sure that we are working off the same data?
 
Upvote 0
The format of all columns appears to be text, though I have formatted it within the macro as well to make sure.

That is causing the problem. The format of a formula cell should never be Text. Use General.
 
Upvote 0
That is causing the problem. The format of a formula cell should never be Text. Use General.
Good catch Rory! I overlooked that.

You can ensure that by placing this line at the top of your code:
Code:
    Columns("K:K").NumberFormat = "General"
 
Upvote 0
Thanks, fellas...that did the trick. I was sure I had it formatted as general the first time I tried it, so I switched to text. You'd think after years of working with VLookups I'd have the general vs text issue ingrained by now.
 
Upvote 0
You are welcome.
Glad we could help!:)
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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
Back
Top