Condition if Cell contains anything then write formula

Infine

Board Regular
Joined
Oct 16, 2019
Messages
93
Office Version
  1. 365
Platform
  1. Windows
Hello,

I want to be able to write out this function: "=VLOOKUP(D2;Databas!$A$2:$O$1048576;8;FALSE)" (which will automatically change "D2 to D3, D4 etc depending on which cell you write in on D)


So if D2 contains ANYTHING I want "=VLOOKUP(D2;Databas!$A$2:$O$1048576;8;FALSE)" be typed next to it (on E2). If it does not contain anything, I want E2 be empty. Is this possible? Can I put this through some Rule?

If D3 contains ANYTHING I want "=VLOOKUP(D3;Databas!$A$2:$O$1048576;8;FALSE)" be typed next to it (on E3) etc.

Appreciate all help.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi Infine
Put this in the sheets module and save the workbook as .xlsm

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 4 Then
        If Target = "" Then
            Target.Offset(, 1) = ""
        Else
            Target.Offset(, 1).Formula = "=VLOOKUP(" & Target.Address(0, 0) & ",Databas!$A$2:$O$1048576,8,FALSE)"
        End If
    End If
End Sub
 
Upvote 0
Is there no other way than using VBA as I do not want to run a SUB everytime as people who will use this will not be able to do this without causing anything major.

I was more thinking for a Conditional rule which says if Cell D2 contains anything, write out in E2 "VLOOKUP bla bla". Is there a way?
 
Upvote 0
Maybe
Excel Formula:
=if(D2="";"";VLOOKUP(D2;Databas!$A$2:$O$1048576;8;FALSE))
 
Upvote 0
How exactly should I do this? What condition should I use, is this below correct? I don't seem to make it work



1610026678621.png




I am thinking: IF D2 contains anything then E2 Types VLOOKUP code
 
Upvote 0
You cannot do that with conditional formatting.
You either put the formula into E2 & copy down as far as needed & it will return "" until a value is put into col D, or you use as macro to add the formula when needed.
 
Upvote 0
Darn, this is not good news :P 2021 and Excel has not made this possible...

We drag it at the moment but this is not optimal. VBA is not optimal either as there is a risk with this data.
 
Upvote 0
Fraid you don't have much choice.

By the way, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Infine
The Vba Above runs very quick (as in no one would evein know its running and if a cell is changed that is not in "D" column, the code does nothing
Up to you but seems the best solution
 
Upvote 0
I see too many risks with a function/Makro for the Database.

However, I might add this on the future. What would be "Target"? Could you describe how I would use the function you have written? I am more familiar with using loops. If I use this script I would add more things to automatically write:

If I type something in D2 or B2 I want in A2: "CTRL + SHIFT ;" / Today() (Todays date in text).
I want that VLOOKUP in E2 and F2 and G2.

And if I type something in D3 or B3 I want the same again for A3, E3, F3, G3 and I want to create a Makro button which every user will have to click to run the Sub.
 
Upvote 0

Forum statistics

Threads
1,215,514
Messages
6,125,271
Members
449,219
Latest member
daynle

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