Install a function in a cell

  • I have a template workbook that has a drop down list for the user to choose a Customer Name. I then have a function [=IFERROR(VLOOKUP(J13,Customer Data!A2:C202,3,FALSE),"")] looks at cell J13 and returns the Customers Street Address on an exact match. I have the validation error turned off so the user can type in a name that may not be in the drop down list.


    My questions is because the function will wipes itself out if the user enters a name not in the list. I'd like to be able to re-insert the function when the template is closed so the next time it opens the function is there.


    The Private Sub Workbook_BeforeClose(Cancel As Boolean) looks like this:



    I hope I've posted this correctly, so if not I apologize in advance.


    tjamestx

  • Code Tags Added
    Your post does not comply with our Forum RULES. Use code tags around code.


    Posting code between

    Code

    tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.


    Highlight your code and click the # icon at the top of your post window.


    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)

  • While placing the formula through VBA, you need to double the double quotes if used in the formula. So the line which inserts the formula in the cell should be like this...

    Code
    Range("J14").Formula = "=IFERROR(VLOOKUP(J13,Sheet2!A2:C202,3,FALSE),[COLOR=#FF0000][B]""""[/B][/COLOR])"

    Regards.
    sktneer

  • Alan, thank you for informing me of the mistake in posting and for correcting it for me.


    sktneer, thank you for fixing my issue with the VBA, its works exactly as I wanted.


    tjamestx

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!