+ Reply to Thread
Results 1 to 3 of 3

How do you insert a custom function into an excel cell using vba?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    How do you insert a custom function into an excel cell using vba?

    I need to put a formula into a column if the column next to it has a value.

    Application.Worksheets(ws).Range("AC"&LastRow).Formula = "=IF(AB2<>"",("ABC(")&(AB2)&(")ABC(")&(Y2)&(");"), "")"
    although I could use some help with the syntax.

    Basically the final formula should look like

    = "some text" & Cell AB2 & "some text" & Y2 & "some text"

    if AB2 has a value. In the code above I get a end of statement error in VBA which I am guessing is because of all the parentheses.

    If anyone can help me with this I'd appreciate it.
    Last edited by 111StepsAhead; 12-06-2011 at 11:06 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,243

    Re: How do you insert a custom function into an excel cell using vba?

    Hi 111StepsAhead,

    I make the formula work in the cell not using VBA first. Then copy the formula to the VBA editor.
    Put quotes in front and the rear and double each quote in the rest of the formula.
    I've spent hours trying to build formulas using VBA and the above is how I've solved many of the syntax errors.

    I hope this helps.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: How do you insert a custom function into an excel cell using vba?

    Yes, thank you MarvinP. That solved my first issue. However it brings to light a new problem. A type mismatch that occurs with the entire line.

    Application.Worksheets(ws).Range("AC" & LastRow).Formula = "=IF(AB2<>"""",""ABC(""&AB2&"")ABC(""&Y2&"");"", "")"
    I am looking for a solution but in if anyone knows the answer I'd appreciate it.

    EDIT: Forgot to double quote the if false statement. Thanks again Marvin.
    Last edited by 111StepsAhead; 12-06-2011 at 11:07 AM.

+ 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