Consulting

Results 1 to 6 of 6

Thread: Convert formula's cell reference into value

  1. #1

    Convert formula's cell reference into value

    Dear Masters,

    I need your help. I want to create an formula that will transform my cell reff into values.

    Eg.

    cell A1 = 2
    cell A2 = 4

    cell A3 = A1+A2

    After running the code the result in cell A3 should look like : cell A3 "= 2+4"

    I had made some searches unfortunately finding unuseful hints ( like macro for copy -> paste or code for transform one column in values plus an other one ... )


    Many thanks,
    Laurentiu

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Not sure I'm following your logic here. You say you "want to create a formula that will transform my cell reff into values".

    Cell references are the column & row headers created by Microsoft. Whereas the values ( 2 & 4) are values created by a user. How did you wish to proceed with this?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I read the OP as asking about a 'description' of how as formula was evaluated. Handy, but likely impossible

    Example:

    A1 = 2
    A2 = 4
    A3 = formula of =A1+A2

    In A4 formula of =MaybeThis(A3)

    Option Explicit
    Function MaybeThis(r As Range) As String
        Dim sFormula As String
        
        sFormula = r.Formula
        
        'this is where it gets sticky
        'hardcoded for simple A3 = "=A1+A2" formula
        MaybeThis = "=" & Range("A1").Value & "+" & Range("A2").Value
    End Function

    How could you handle things like =SUM(A3:COUNTIF(B:B,D6))*L20/2 .... etc.?????


    Paul
    Attached Images Attached Images

  4. #4
    Quote Originally Posted by Paul_Hossler View Post
    I read the OP as asking about a 'description' of how as formula was evaluated. Handy, but likely impossible

    Example:

    A1 = 2
    A2 = 4
    A3 = formula of =A1+A2

    In A4 formula of =MaybeThis(A3)

    Option Explicit
    Function MaybeThis(r As Range) As String
        Dim sFormula As String
        
        sFormula = r.Formula
        
        'this is where it gets sticky
        'hardcoded for simple A3 = "=A1+A2" formula
        MaybeThis = "=" & Range("A1").Value & "+" & Range("A2").Value
    End Function

    How could you handle things like =SUM(A3:COUNTIF(B:B,D6))*L20/2 .... etc.?????


    Paul

    Hi Paul,


    Thanks for your post. I don't have any ranges (A1:A9) i have only simple links with simple operators like +-/*.

    My mistake that i mentioned i want a formula. If it can be done somehow else, great !
    I just need a code that will bring the value of a cell instead of the cell reference (like instead of cell reff A1 we should have "2" )

    Cheers,
    Laurentiu

  5. #5
    VBAX Regular
    Joined
    Jan 2013
    Posts
    84
    Location
    Try this code, with the cell of the formula selected:

       Sub AppendApostrophe()    
        Dim c As Range
        For Each c In Selection
            If c.Value <> "" Then c.Value = "'" & c.Formula
        Next
       End Sub

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •