+ Reply to Thread
Results 1 to 4 of 4

Macro to write formula in cell based on value in different cell

  1. #1
    Registered User
    Join Date
    02-18-2014
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    2

    Macro to write formula in cell based on value in different cell

    I want to create a macro that uses the values from cell A1 and A2 to write a formula in cell A3. It is important that the formula in A3 contains the actual numbers from A1 and A2. I don't want the forumula to just be "=A1+A2", but rather "=2.50+3.75" (if those are the values of A1 and A2 at the time of running the macro).

    This macro works, but does not give me the decimals from A1 and A2 in the formula of A3. Tried to dim as single or double instead, but that only returns run time error 1004 Method 'Formula' of object 'Range' failed. Why?

    Please Login or Register  to view this content.
    Last edited by AliGW; 07-02-2022 at 01:44 AM. Reason: Code tags added.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,830

    Re: Macro to write formula in cell based on value in different cell

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. As you are new here, I have done it for you this time. I've also moved the thread to the correct forum section.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,202

    Re: Macro to write formula in cell based on value in different cell

    this worked for me ...

    Please Login or Register  to view this content.
    result in formula bar

    =2.65+4.09

    (6.74)
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  4. #4
    Registered User
    Join Date
    02-18-2014
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Macro to write formula in cell based on value in different cell

    Thank you, both of you. Unfortunately I am still getting the same error message. I'm starting to think it is related to the use of "," as decimal separator?

    Macro works when values in A1 and A2 are whole numbers, but not when they have decimals separated with commas.
    Last edited by Stue; 07-02-2022 at 02:53 AM.

+ 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. [SOLVED] how to write a specific word in a cell based on other cell
    By emmr in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-08-2019, 04:10 AM
  2. Replies: 4
    Last Post: 09-26-2015, 06:44 PM
  3. VB Macro to write formula in a cell
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-06-2014, 09:19 AM
  4. Need to write a formula based on fontcolor of cel and cell color
    By atzerv in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-18-2014, 04:25 PM
  5. [SOLVED] Macro to write a formula with dynamic cell references
    By alpha608 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-27-2013, 12:18 PM
  6. [SOLVED] Formula to write multiple values to a cell based on value of other cells
    By crazyjockey in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-05-2012, 09:32 PM
  7. Tiny macro won't write cell value based on comparing dates
    By Poulan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-10-2010, 12:42 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