+ Reply to Thread
Results 1 to 9 of 9

formula in vba

  1. #1
    Registered User
    Join Date
    03-07-2012
    Location
    Reading, England
    MS-Off Ver
    Excel 2010
    Posts
    99

    formula in vba

    Hi all,

    Please advise what I am doing wrong.

    In column Z I want to add the following formula which looks up values in sheet "Jan 18"

    =IF(ISERROR(VLOOKUP(A2,'Jan 18'!A:A,1,FALSE)),"New Employee","OK") and works ok as a formula in the spreadsheet, but as soon as I put it in vba code it gives a syntax error:

    With ActiveSheet.Range("Z2:Z" & LastRow)
    .Formula = "IF(ISERROR(VLOOKUP(A2,'Jan 18'!A:A,1,FALSE)),"New Employee","OK")"
    End With

    Thanks!

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: formula in vba

    Try
    Please Login or Register  to view this content.
    A string in VBA requires to be enclosed in " "
    - which causes a problem if you want to put " inside the string
    - so the solution is to double them up

    Also you omitted the "=" at the beginning
    Last edited by kev_; 03-12-2018 at 07:01 AM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Registered User
    Join Date
    03-07-2012
    Location
    Reading, England
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: formula in vba

    Thanks kev_!

  4. #4
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: formula in vba

    Please mark thread as SOLVED (ThreadTools@TopOfThread)

  5. #5
    Registered User
    Join Date
    03-07-2012
    Location
    Reading, England
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: formula in vba

    Hi,

    Could you please what I am doing wrong again. I put an input box to invite a user to enter worksheet name (in this case the user will enter Jan 18 which a name of a sheet in the same workbook)

    and then I wanted to use that worksheet name in the below formula, but the macro tried to open some window to search for files and after I closed that window I got formula with Wsheet in the cell, not Jan 18


    WSheet = InputBox("Enter Worksheet for lookup")

    With ActiveSheet.Range("Z2:Z" & LastRow)
    .Formula = "=IF(ISERROR(VLOOKUP(A2, WSheet!A:A,1,FALSE)),""New Employee"",""OK"")"
    End With

    Result was this
    =IF(ISERROR(VLOOKUP(A2, WSheet!A:A,1,FALSE)),"New Employee","OK")

    But I want this:
    =IF(ISERROR(VLOOKUP(A2, 'Jan 18'!A:A,1,FALSE)),"New Employee","OK")

    Please advise

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: formula in vba

    You're putting the variable name into the formula. To get the variable value, you must concatenate
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  7. #7
    Registered User
    Join Date
    03-07-2012
    Location
    Reading, England
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: formula in vba

    Brilliant! Thanks!

  8. #8
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: formula in vba

    Please Login or Register  to view this content.
    Concatenate the different elements together
    - wSheet is a variable holding a string and so it needs to be integrated with & on either side
    - then you ned to teminate the preceding string with " and introduce a " to begin the followingh string
    Last edited by kev_; 03-12-2018 at 08:07 AM.

  9. #9
    Registered User
    Join Date
    03-07-2012
    Location
    Reading, England
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: formula in vba

    Thanks Kev!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 02-16-2018, 06:50 AM
  2. Replies: 8
    Last Post: 09-22-2017, 05:41 AM
  3. Excel formula bar to display the result of the formula , not the formula?
    By max_max in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-18-2016, 07:15 PM
  4. Replies: 11
    Last Post: 06-06-2014, 03:34 PM
  5. how to hide formula in formula box, view lookup result in formula box?
    By vengatvj in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-14-2013, 04:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1