+ Reply to Thread
Results 1 to 6 of 6

Putting a formula into a cell where cells vary

  1. #1
    Registered User
    Join Date
    03-09-2017
    Location
    Glasgow, Scotland
    MS-Off Ver
    2013
    Posts
    6

    Putting a formula into a cell where cells vary

    Private Sub CommandButton1_Click()
    Dim NumMembers As Integer
    NumMembers = Cells(2, 2).Value
    Dim ConCheck As Integer
    Dim Con1 As Integer
    Dim Con2 As Integer


    For ConCheck = 1 To NumMembers


    Con1 = Cells(4 + ConCheck, 12)
    Con2 = Cells(4 + ConCheck, 13)


    If Cells(4 + Con1, 3) = "Fixed" Then GoTo Fixed 'check if the bearing is fixed

    Cells(4 + Con1, 8).Formula = (Cells(4 + ConCheck, 11) ^ 2 - (Cells(4 + Con2, 9) + Cells(4 + Con1, 9)) ^ 2) ^ 0.5 - Cells(4 + Con2, 8) 'print Con1(x)
    Cells(4 + Con1, 9).Formula = (Cells(4 + ConCheck, 11) ^ 2 - (Cells(4 + Con2, 8) + Cells(4 + Con1, 8)) ^ 2) ^ 0.5 - Cells(4 + Con2, 9) 'print Con1(y)
    Cells(4 + Con2, 8).Formula = (Cells(4 + ConCheck, 11) ^ 2 - (Cells(4 + Con1, 9) + Cells(4 + Con2, 9)) ^ 2) ^ 0.5 - Cells(4 + Con1, 8) 'print Con2(x)
    Cells(4 + Con2, 9).Formula = (Cells(4 + ConCheck, 11) ^ 2 - (Cells(4 + Con1, 8) + Cells(4 + Con2, 8)) ^ 2) ^ 0.5 - Cells(4 + Con1, 9) 'print Con2(y)

    Fixed: Next ConCheck

    End Sub



    This is my script I have written to attempt to input formulae into cells. The issue that I have is keep the formula in the cell instead of the value as the value changes as more equations are solved but when I put the "" marks round I obviouosly just get what is written above in the cell. I have found it difficult to explain this but basically would like to code in a way that the variables that correspond to cells become fixed to that cell when the formula prints into the cell e.g.

    Cells(4 + Con1, 8).Formula = (Cells(4 + ConCheck, 11) ^ 2 - (Cells(4 + Con2, 9) + Cells(4 + Con1, 9)) ^ 2) ^ 0.5 - Cells(4 + Con2, 8)

    becomes

    Cells(4 + Con1, 8).Formula = "((K5) ^ 2 - (I6 + I7)) ^ 2) ^ 0.5 - H6"

    so that formula stays in that cell but the VBA script can still alter the formula dependant on the variables.

    hope this makes sense please help!

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,446

    Re: Putting a formula into a cell where cells vary

    Hi,

    Could you use the address of each of the variable cell references?

    eg...

    Please Login or Register  to view this content.
    NB Please use code tags - take a look at the forum rules to show you how.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    03-09-2017
    Location
    Glasgow, Scotland
    MS-Off Ver
    2013
    Posts
    6

    Re: Putting a formula into a cell where cells vary

    sorry about that new to this!

    but that worked a dream however, there's often 2 or more of these equation being entered into the one cell. I expected this, do you know any ways to solve them to give a value into the cell, all while keeping the formula there so if I were to change a cell on the spreadsheet it would change all corresponding cells?

    My VBA knowledge is limited. and I realise this is complex so any help is welcomed!

  4. #4
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,446

    Re: Putting a formula into a cell where cells vary

    I'm not sure what you mean - can you give an example (an attached example worksheet would be most helpful)?

  5. #5
    Registered User
    Join Date
    03-09-2017
    Location
    Glasgow, Scotland
    MS-Off Ver
    2013
    Posts
    6

    Re: Putting a formula into a cell where cells vary

    so the spreadsheet is used to calculate positions of bearings in a large machine using a coordinate system. The end goal is to have these coordinates in the cells but as formula so if i wish to see how a change in length alters the position of the bearings. This has all been done however the system as is tries to put multiple formulas into the one cell but the system needs these to solve it as one is not enough. I do wonder if the VBA will stretch this far and be able to do this.
    The spreadsheet is poor and try avoid hitting the "Insert" button as it is buggy and wipes entered data. Really struggling to see where to attach the file??????

  6. #6
    Registered User
    Join Date
    03-09-2017
    Location
    Glasgow, Scotland
    MS-Off Ver
    2013
    Posts
    6

    Re: Putting a formula into a cell where cells vary

    got it. sorry about that.
    Attached Files Attached Files

+ 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: 19
    Last Post: 04-12-2017, 09:42 AM
  2. [SOLVED] VBA - putting a formula in cells but its got a syntax error please help!! :-)
    By LauraWork in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-27-2013, 08:06 AM
  3. [SOLVED] Help with NOW() Formula to vary results in different cells
    By nenadmail in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-29-2012, 04:28 PM
  4. Replies: 4
    Last Post: 04-12-2012, 01:10 PM
  5. Adjust my formula to vary output based upon Y or N in cell
    By jwbeaty in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-22-2011, 04:45 PM
  6. Putting multiplication formula in cells with vba
    By bj33790 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-18-2010, 08:43 AM
  7. Replies: 2
    Last Post: 07-21-2005, 03:05 PM
  8. [SOLVED] Vary formula based on what another cell contains
    By MMH in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2005, 05:05 PM

Tags for this Thread

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