+ Reply to Thread
Results 1 to 22 of 22

vba to extract formula with values

Hybrid View

freeriding vba to extract formula with... 10-17-2023, 01:09 PM
Barieq Re: vba to extract formula... 10-17-2023, 04:54 PM
freeriding Re: vba to extract formula... 10-17-2023, 05:16 PM
Bernie Deitrick Re: vba to extract formula... 10-17-2023, 05:01 PM
freeriding Re: vba to extract formula... 10-17-2023, 05:17 PM
Bernie Deitrick Re: vba to extract formula... 10-17-2023, 06:04 PM
freeriding Re: vba to extract formula... 10-17-2023, 06:24 PM
Bernie Deitrick Re: vba to extract formula... 10-17-2023, 06:37 PM
Muhammad Fraz Re: vba to extract formula... 10-17-2023, 06:45 PM
freeriding Re: vba to extract formula... 10-17-2023, 06:56 PM
Muhammad Fraz Re: vba to extract formula... 10-17-2023, 07:03 PM
Bernie Deitrick Re: vba to extract formula... 10-17-2023, 07:56 PM
freeriding Re: vba to extract formula... 10-17-2023, 08:03 PM
TMS Re: vba to extract formula... 10-17-2023, 08:31 PM
TMS Re: vba to extract formula... 10-17-2023, 08:34 PM
freeriding Re: vba to extract formula... 10-17-2023, 08:38 PM
TMS Re: vba to extract formula... 10-17-2023, 08:44 PM
freeriding Re: vba to extract formula... 10-20-2023, 12:42 PM
Bernie Deitrick Re: vba to extract formula... 10-20-2023, 04:35 PM
freeriding Re: vba to extract formula... 10-20-2023, 06:29 PM
  1. #1
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    vba to extract formula with values

    Hallo! I have a workbook with multiple worksheets.

    Is there a vba so i can extract the formula with values from a cell.

    eg. lets say there are 2 worksheets: 1 and 2.

    A1 in worksheet 1 has a formula that gives as a result 1 and

    A1 in worksheet 2 has a formula that gives as a result 2


    a formula in a cell in worksheet 2 is: = 1!A1 + A1, so i want it to display: 1+2

    Is there a vba that works like that?

  2. #2
    Forum Contributor
    Join Date
    11-12-2012
    Location
    Jeddah, Saudi Arabia
    MS-Off Ver
    2010, 2013, 2016, Office 365
    Posts
    521

    Re: vba to extract formula with values

    try using formula

    =CONCATENATE(Sheet1!A1,"+",Sheet2!A1)

  3. #3
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    Re: vba to extract formula with values

    Quote Originally Posted by Barieq View Post
    try using formula

    =CONCATENATE(Sheet1!A1,"+",Sheet2!A1)
    Thanks, it works, but too much manual work for many cells, with complex formulas.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,302

    Re: vba to extract formula with values

    This will work - maybe - as long as your sheet names don't have spaces - written to process just the activecell:

    Option Explicit
    
    Sub ReplaceReferences()
        Dim f As String
        Dim strF As String
        Dim i As Integer
        Dim strR As String
        Dim v As Variant
        
        strR = "+-*&^=><,/"
        strF = ActiveCell.Formula
        
        f = ActiveCell.Formula
        For i = 1 To Len(strR)
            f = Replace(f, Mid(strR, i, 1), " ")
        Next i
        
        v = Split(f, " ")
        
        For i = LBound(v) To UBound(v)
            If InStr(v(i), "!") > 0 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        For i = LBound(v) To UBound(v)
            If InStr(v(i), ":") > 0 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        For i = LBound(v) To UBound(v)
            If Len(v(i)) > 1 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        
        ActiveCell.Formula = "'" & Right(strF, Len(strF) - 1)
        
        
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  5. #5
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    Re: vba to extract formula with values

    Quote Originally Posted by Bernie Deitrick View Post
    This will work - maybe - as long as your sheet names don't have spaces - written to process just the activecell:

    Option Explicit
    
    Sub ReplaceReferences()
        Dim f As String
        Dim strF As String
        Dim i As Integer
        Dim strR As String
        Dim v As Variant
        
        strR = "+-*&^=><,/"
        strF = ActiveCell.Formula
        
        f = ActiveCell.Formula
        For i = 1 To Len(strR)
            f = Replace(f, Mid(strR, i, 1), " ")
        Next i
        
        v = Split(f, " ")
        
        For i = LBound(v) To UBound(v)
            If InStr(v(i), "!") > 0 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        For i = LBound(v) To UBound(v)
            If InStr(v(i), ":") > 0 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        For i = LBound(v) To UBound(v)
            If Len(v(i)) > 1 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        
        ActiveCell.Formula = "'" & Right(strF, Len(strF) - 1)
        
        
    End Sub
    Thank you, i'll try it, but what is the formula that i enter in order to use this vba module?

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,302

    Re: vba to extract formula with values

    If you want it as a function, then try this code, used like

    =ReplaceReferences(A2)

    where A2 has the formula you want evaluated.

    Option Explicit
    
    Function ReplaceReferences(r As Range) As String
        Dim f As String
        Dim strF As String
        Dim i As Integer
        Dim strR As String
        Dim v As Variant
        
        strR = "+-*&^=><,/"
        strF = r.Formula
        
        f = r.Formula
        For i = 1 To Len(strR)
            f = Replace(f, Mid(strR, i, 1), " ")
        Next i
        
        v = Split(f, " ")
        
        For i = LBound(v) To UBound(v)
            If InStr(v(i), "!") > 0 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        For i = LBound(v) To UBound(v)
            If InStr(v(i), ":") > 0 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        For i = LBound(v) To UBound(v)
            If Len(v(i)) > 1 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        
        ReplaceReferences = "'" & Right(strF, Len(strF) - 1)
        
        
    End Sub

  7. #7
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    Re: vba to extract formula with values

    Thanks, i get this message

    r.png

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,302

    Re: vba to extract formula with values

    Sorry - I should have told you to remove the old subroutine code, or comment it out.

  9. #9
    Registered User
    Join Date
    11-29-2022
    Location
    Lahore, Pakistan
    MS-Off Ver
    MS Office 2007
    Posts
    70

    Re: vba to extract formula with values

    Quote Originally Posted by Bernie Deitrick View Post
    Sorry - I should have told you to remove the old subroutine code, or comment it out.
    Sir, If you have a free time, then please look into my thread too available in your visitor's messages. Thanks.

  10. #10
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    Re: vba to extract formula with values

    I didn't save the routine in your third message
    https://www.excelforum.com/excel-pro...ml#post5881955

    This message is there unfortunately. I am be doing something wrong.

    I noticed also that macros are blank. Shouldn't there be a macro name replacereferences?

    rr.png
    Last edited by freeriding; 10-17-2023 at 07:05 PM.

  11. #11
    Registered User
    Join Date
    11-29-2022
    Location
    Lahore, Pakistan
    MS-Off Ver
    MS Office 2007
    Posts
    70

    Re: vba to extract formula with values

    Quote Originally Posted by freeriding View Post
    I didn't save the routine in your third message
    https://www.excelforum.com/excel-pro...ml#post5881955

    This message is there unfortunately. I am be doing something wrong.
    Just Change the Last line
    End Sub
    to
    End Function

  12. #12
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    Re: vba to extract formula with values

    Thank you, it works fine!!!
    Just doesn't work for worksheets that contain space in their name.

  13. #13
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,302

    Re: vba to extract formula with values

    D'oh! Thank you for catching that!

  14. #14
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    Re: vba to extract formula with values

    I noticed that you remarked it, i just confirm it.
    Is there a solution so that space containing worksheets don't create a problem?

  15. #15
    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
    48,538

    Re: vba to extract formula with values

    Try this variation on Bernie's function. If a worksheetname has spaces, you can specify a delimiter, for example "|"

    Option Explicit
    
    Function ReplaceReferences(r As Range, Optional delim As String = " ") As String
        
    ' Call using, for example, =ReplaceReferences(B1)
    ' Call using, for example, =ReplaceReferences(B1," ")
    ' Call using, for example, =ReplaceReferences(B1,"|")
        
        Dim f As String
        Dim strF As String
        Dim i As Integer
        Dim strR As String
        Dim v As Variant
        
        strR = "+-*&^=><,/"
        strF = r.Formula
        
        f = r.Formula
        For i = 1 To Len(strR)
            f = Replace(f, Mid(strR, i, 1), delim)
        Next i
        
        v = Split(f, delim)
        
        For i = LBound(v) To UBound(v)
            If InStr(v(i), "!") > 0 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        For i = LBound(v) To UBound(v)
            If InStr(v(i), ":") > 0 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        For i = LBound(v) To UBound(v)
            If Len(v(i)) > 1 Then
                strF = Replace(strF, v(i), Evaluate("=" & v(i)))
            End If
        Next i
        
        
        ReplaceReferences = "'" & Right(strF, Len(strF) - 1)
    
    End Function
    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


  16. #16
    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
    48,538

    Re: vba to extract formula with values

    I don't think you need the single quote:
        ReplaceReferences = Right(strF, Len(strF) - 1)

  17. #17
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    Re: vba to extract formula with values

    Thank you!

  18. #18
    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
    48,538

    Re: vba to extract formula with values

    You're welcome.



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

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  19. #19
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    Re: vba to extract formula with values

    Hallo, when the formula is like =(A-B)*C, why does it show #value?

  20. #20
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,302

    Re: vba to extract formula with values

    Because this line:

    strR = "+-*&^=><,/"

    does not include ( and ).

    I did that so that something like

    =SUM(A:A)

    would not blow up the function by trying to include all of A in the result, but would show the SUM() result.

    You can change that line to this to fix your issue:

    strR = "+-*&^=><,/()"

  21. #21
    Forum Contributor
    Join Date
    09-21-2015
    Location
    Alexandria
    MS-Off Ver
    2013
    Posts
    106

    Re: vba to extract formula with values

    Thank you!!!

+ 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. Formula to extract unique values/distinct values in rows in excel
    By bjnockle in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-07-2020, 05:35 PM
  2. Non Array Formula to Extract Values
    By bjnockle in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 03-31-2018, 04:50 AM
  3. Replies: 11
    Last Post: 10-27-2016, 07:33 PM
  4. Extract Values using formula
    By bjnockle in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-21-2016, 09:32 AM
  5. [SOLVED] Extract values (row values and column values) with formula
    By bjnockle in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 08-17-2014, 10:13 AM
  6. [SOLVED] Formula to extract values
    By Buster^ in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-21-2014, 05:09 AM
  7. Formula to Extract Values within a Range
    By prkhan56 in forum Excel General
    Replies: 3
    Last Post: 07-21-2005, 02:05 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