+ Reply to Thread
Results 1 to 5 of 5

Formula works in excel, but not in VBA

  1. #1
    Registered User
    Join Date
    04-29-2014
    Location
    Florence, AZ
    MS-Off Ver
    Excel 2013
    Posts
    6

    Post Formula works in excel, but not in VBA

    I am trying to use a formula in a macro to find only number values greater than 89999.
    The list is made of job numbers in the 70-80000's for regular jobs, and 90,000 for jobs that should be capitalized. The problem is that some of the 70 & 80000 job numbers have -1, -2 (e.g. 73500-1, etc). The "dash" job numbers remain stored as text whereas the rest are stored as number.

    When I input the following formula in excel, it works perfectly:
    =IF(AND(ISNONTEXT(B2),((B2>89999))),"Yes","No")
    It tells me "Yes" if the job is greater than 90000, and if not, (wether it is less than 89999 or text) it says "No". This helps me then generate a pivot table that pulls only the 90000 jobs.

    BUT... When I enter it on my macro as this:

    ActiveCell.FormulaR1C1Local= "=IF(AND(ISNONTEXT(B2),((B2>89999))),"Yes","No")"

    I get a "Compiling error, expected end of statement" with the word "Yes" highlighted.

    How can I make formula work from a macro?



    Thanks in advance!

    Eli

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,036

    Re: Formula works in excel, but not in VBA

    You need double quotation.
    Try:

    Please Login or Register  to view this content.
    Never use Merged Cells in Excel

  3. #3
    Registered User
    Join Date
    04-29-2014
    Location
    Florence, AZ
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Formula works in excel, but not in VBA

    Well.. I added the double quotation but now I get #NAME? in each cell.
    Last edited by ELIVERGARA; 04-30-2014 at 04:41 PM.

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Formula works in excel, but not in VBA

    Hi,

    If you want to refer to cell B2, then do not use the R1C1 notation. Your code should be :

    Please Login or Register  to view this content.
    If you want to use R1C1 notation, then it should be :
    Please Login or Register  to view this content.
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  5. #5
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Formula works in excel, but not in VBA

    ... and let's say you want to copy the formula in column C, then using the R1C1 notation, it would be :

    Please Login or Register  to view this content.
    (Adapt the range as required)

+ 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: 0
    Last Post: 03-19-2012, 07:32 PM
  2. Excel Formula Only Works in First Two Rows
    By woogster in forum Excel General
    Replies: 7
    Last Post: 09-10-2009, 03:56 PM
  3. Formula works in Excel but not VB
    By johncassell in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2007, 01:02 PM
  4. [SOLVED] Excel Addin works that works on a template workbook
    By s.jay_k in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-15-2006, 03:35 PM
  5. [SOLVED] How do I convert works file to excel without works software?
    By CatMB in forum Excel General
    Replies: 1
    Last Post: 06-21-2005, 12:05 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