+ Reply to Thread
Results 1 to 12 of 12

How to write VBA for Excel function?

  1. #1
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    How to write VBA for Excel function?

    For example, Activesheet.Cells(15, 18).Value = "MSFT", where 18 is column R

    I would like to write ActiveSheet.Cells(16, 18).Formula = "=STOCKHISTORY(ActiveSheet.Cells(15, 18).Value,TODAY()-30,TODAY())", something like that, how to write syntax correctly? There is error in my code, it turns out to be =@STOCKHISTORY(@ActiveSheet.Cells(15, 18).Value,TODAY()-30,TODAY()) Actually, I want Cell R16 turn out to be = STOCKHISTORY("MSFT",TODAY()-30,TODAY()), but I cannot use MSFT directly, how to make Activesheet.Cells(15, 18).Value as part of code.

    I need to write it in this way, since there are a list of companies, I have to write a VBA loop. Cells(15, 18), Cells(15, 21), Cells(15, 24), Cells(15, 27), ........

    To make my question clear: At the end, it should look something similar to attached screenshot, and I would like to write the function =STOCKHISTORY(ActiveSheet.Cells(15, 18).Value,TODAY()-30,TODAY()) in VBA code. How?

    Thanks.
    Attached Images Attached Images
    Last edited by VAer; 09-01-2021 at 12:27 AM.

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: How to write VBA for Excel function?

    Try this

    "=STOCKHISTORY(R15,TODAY()-30,TODAY())"
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: How to write VBA for Excel function?

    Try:
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,915

    Re: How to write VBA for Excel function?

    Something like:

    Please Login or Register  to view this content.
    Rory

  5. #5
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Re: How to write VBA for Excel function?

    Quote Originally Posted by TMS View Post
    Try:
    Please Login or Register  to view this content.
    It does not work. It ends up with =@STOCKHISTORY(@MSFT,TODAY()-30,TODAY()) , as I mentioned in my post. The cell appears as #NAME?

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,915

    Re: How to write VBA for Excel function?

    You need to add quotes if you're going to do it that way:

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Re: How to write VBA for Excel function?

    Quote Originally Posted by kvsrinivasamurthy View Post
    Try this

    "=STOCKHISTORY(R15,TODAY()-30,TODAY())"
    First, it does not work (I tried one cell only). Secondly, I don't want to use R15, I would like to use ActiveSheet.Cells(15, 18).Value. Eventuallly, it will be a For Loop

  8. #8
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Re: How to write VBA for Excel function?

    Quote Originally Posted by rorya View Post
    Something like:

    Please Login or Register  to view this content.

    Thanks. But I don't understand the code R1C1/R1C5. Secondly, I don't know how many columns there. I have a list of companied, LastRowC = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row, the number of companies = LastRowC - 1 (heading title row), then I put each symbol in the cell Activesheet.Cells(15, 18), Activesheet.Cells(15, 21), Activesheet.Cells(15, 24)......

  9. #9
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Re: How to write VBA for Excel function?

    Quote Originally Posted by rorya View Post
    You need to add quotes if you're going to do it that way:

    Please Login or Register  to view this content.
    Thanks for correction, it is """" for using double quote in VBA. I have tried this one, it turns out better, but still not right. It ends up with =@STOCKHISTORY("MSFT",TODAY()-30,TODAY()) the cell appears as Date but no data

    There is something wrong with syntax, it already turns out to have @ as result

  10. #10
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,915

    Re: How to write VBA for Excel function?

    Use .Formula2 instead of .Formula

  11. #11
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Re: How to write VBA for Excel function?

    Quote Originally Posted by rorya View Post
    Use .Formula2 instead of .Formula
    Thanks. It seems below code working.

    ActiveSheet.Cells(16, 18).Formula2 = "=STOCKHISTORY(" & """" & ActiveSheet.Cells(15, 18).Value & """" & ",TODAY()-30,TODAY())"

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: How to write VBA for Excel function?

    Eventuallly, it will be a For Loop
    Something like this maybe.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

+ 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. Write A+B=B+A with Excel Function
    By Yami_Excel in forum Excel General
    Replies: 4
    Last Post: 11-15-2019, 06:19 AM
  2. [SOLVED] Write Sub procedure to format excel and write case/if statement to text file
    By vbronton in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-06-2018, 08:26 AM
  3. Excel hangs when trying to use addfromstring function to write a macro
    By gows1982 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2014, 03:29 AM
  4. How to write Macros function to open other Excel files?
    By AP Accountan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-21-2013, 12:52 PM
  5. Replies: 10
    Last Post: 12-10-2012, 03:26 PM
  6. How do I write this Function using Excel's IF
    By Unsafe in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-27-2007, 05:42 PM
  7. [SOLVED] How to write IF function in Excel 2000 when there are two conditi.
    By Dajana in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-26-2005, 11:06 AM

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