+ Reply to Thread
Results 1 to 4 of 4

Formula not working when put inside VBA macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2013
    Posts
    104

    Formula not working when put inside VBA macro

    Hello,

    I am using 2 formulas to report the quarters and fiscal years of dates found in column A (starting at A2) in Excel 2010. The formulas are working perfectly in Excel but when I try to incorpoate them in a VBA macro, the first formula works fine but the second fomula does not work (I get run time errror 13 type mismatch). I think the error might be that VBA does not accept &"-" in a formula? Is there a work around this? Any help would be appreciated. Here is my VBA code:

    Sub testformula()

    With Range("A2", Range("A" & Rows.Count).End(xlUp)).Offset(, 1)
    .Formula = "=CHOOSE(MONTH(A2),4,4,4,1,1,1,2,2,2,3,3,3)"
    .Value = .Value
    End With

    With Range("A2", Range("A" & Rows.Count).End(xlUp)).Offset(, 2)
    .Formula = "=IF(MONTH(A2)<4,YEAR(A2)-1&"-"&RIGHT(YEAR(A2),4),YEAR(A2)&"-"&RIGHT(YEAR(A2)+1,4))"
    .Value = .Value
    End With

    End Sub

  2. #2
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,156

    Re: Formula not working when put inside VBA macro

    hi Chrispelletier, welcome to the forum. do remember to put the code tags when you use codes here. it's the # sign. it should look like below:
    Sub test()
    
    With Range("A2", Range("A" & Rows.Count).End(xlUp)).Offset(, 2)
    .Formula = "=IF(MONTH(A2)<4,YEAR(A2)-1&"" - ""&RIGHT(YEAR(A2),4),YEAR(A2)&"" - ""&RIGHT(YEAR(A2)+1,4))"
    .Value = .Value
    End With
    
    End Sub
    when putting in formulas with double quotes, you must repeat them. otherwise, VBA thinks you are ending the formula since you started with
    "=IF

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  3. #3
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Ottawa, Ontario
    MS-Off Ver
    Excel 2013
    Posts
    104

    Re: Formula not working when put inside VBA macro

    Thanks Benishiryo for your quick response and explanations, it is working great now! I'll make sure I use the code tags next time.

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Formula not working when put inside VBA macro

    Its the double-quotes.

    What is the worksheet formula you are trying to insert?
    Gary's Student

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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