Consulting

Results 1 to 5 of 5

Thread: Printing equation into cell

  1. #1
    VBAX Newbie
    Joined
    May 2010
    Posts
    2
    Location

    Printing equation into cell

    I'm trying to print an equation into a cell. But when I add the = character, it gives runtime-error 1004. What is this about?

    nimim = ThisWorkbook.Name

    Range("B8").Value = "=HLOOKUP([" + nimim + "]Esitietolista!A$1;[" + nimim + "]Kielet!D:H;Telat!A109;FALSE)"

    or

    Range("B8").Value = Chr(61) + "HLOOKUP([" + nimim + "]Esitietolista!A$1;[" + nimim + "]Kielet!D:H;Telat!A109;FALSE)"
    Last edited by tms_; 05-11-2010 at 02:08 AM.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    try:
    [VBA]Range("B8").Formula = "=HLOOKUP('[" + nimim + "]Esitietolista'!A$1;'[" + nimim + "]Kielet'!D:H;Telat!A109;FALSE)"
    [/VBA]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Newbie
    Joined
    May 2010
    Posts
    2
    Location
    It gives the same error...

    Edit. Please note, that nimim = ThisWorkbook.Name
    The code is meant to work on embedded worksheet and link to "base worksheet"
    Last edited by tms_; 05-11-2010 at 02:09 AM.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by tms_
    Please note, that nimim = ThisWorkbook.Name
    The code is meant to work on embedded worksheet and link to "base worksheet"
    1. ..and these worksheets, which workbooks are they in?
    2. Which worksheet and workbook is the cell B8 in?
    3. Which workbook is the code in? (this is important for "nimim = ThisWorkbook.Name")
    4. What kind of module is the code in? (Standard code module/sheet code module/Thisworkbook code module/userform code module/class code module)
    5. Are all worksheet names (a) correctly spelt and (b) do they all exist?
    6. Have you saved at least once, the workbook referred to in "nimim = ThisWorkbook.Name"?

    Please explicitly answer each of the above 1 to 6 (a and b)

    One thing you can do is when you get the error, choose debug and in the Immediate pane, type:
    [vba]?"=HLOOKUP('[" + nimim + "]Esitietolista'!A$1;'[" + nimim + "]Kielet'!D:H;Telat!A109;FALSE)"[/vba](or whatever your code is at the time) and press Enter. Does it look as you expect? If so, copy it and try to paste it directly into the cell. (If all you see is the string when you've done that, edit the cell without changing anything and press Enter.) What does it complain about?

    And one more question, you did try the whole line I suggested earlier, not just a change from .value to .formula?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi tms_

    Well I don't know about your use of + vs & and your use of ; vs , that I am used to. However, you're trying to do a lookup in two sheets? Can't be done as far as I know

    =HLOOKUP( [THISWORKBOOK]Esitietolista!A$1;[THISWORKBOOK]Kielet!D:H;[SOMEOTHERWORKBOOK_cause_the_name_was_left_out]Telat!A109;FALSE)"

    is not going to work.

    Try putting in the variable for the last sheet. At least it worked for me...

    Call me crazy but the Excel Lookups return a value form the table you specify, not from somewhere else...

    I opened book1.xls and put this code in to write to Book4.xls

    [VBA]

    Option Explicit

    Sub PutFormula()

    Dim nimim As String
    Dim WhereFormulaGoes As Worksheet

    nimim = ThisWorkbook.Name

    Set WhereFormulaGoes = Workbooks("Book4.xls").Sheets(1)

    WhereFormulaGoes.Range("B8") = "=HLOOKUP([" & nimim & "]Esitietolista!A$1,[" & nimim & "]Kielet!D:H,[" & nimim & "]Telat!A109,FALSE)"

    End Sub

    [/VBA]

    worked for me
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •