+ Reply to Thread
Results 1 to 8 of 8

Convert String from Excel Range.Value to Equation in VBA

  1. #1
    Forum Contributor
    Join Date
    08-14-2013
    Location
    Florida
    MS-Off Ver
    Excel 2013
    Posts
    105

    Convert String from Excel Range.Value to Equation in VBA

    I have some variables stored as strings in worksheets that I would like to do calculations on in VBA after I define the variables as values.

    Example; In cell A1, I have... "((Pmat * 5) + (Pmdf + 150) - (Zmdf * 1.5)) / 2"

    Pmat, Pmdf, and Zmdf are all variables that are assigned numbers in VBA. For instance let; Pmat = 10, Pmdf = 45, and Zmdf = 20. I would end up with 107.5 as my value.

    However I can't simply do; X = Range.Value
    If I pull A1 value into VBA as "Formula = Range.Value ". What should I Dim Formula as so that VBA knows part of the info in that range is variables and other parts are part of the equation?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Convert String from Excel Range.Value to Equation in VBA

    Where do you want to calculate this formula?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Convert String from Excel Range.Value to Equation in VBA

    I would use a two-dimensional array to hold the variables. (This facilitates replacing substrings with values) See the attached.
    Attached Files Attached Files
    Gary's Student

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Convert String from Excel Range.Value to Equation in VBA

    Try
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    08-14-2013
    Location
    Florida
    MS-Off Ver
    Excel 2013
    Posts
    105

    Re: Convert String from Excel Range.Value to Equation in VBA

    Quote Originally Posted by Jakobshavn View Post
    I would use a two-dimensional array to hold the variables. (This facilitates replacing substrings with values) See the attached.
    This works. Will be confusing keeping track of what position in the array means what. Instead of having Pmat = 5, I would have to know it as ary(1, 2). I will end up having over 70 variable names. I am trying to avoid looping though all of them.

    I do like the MultiArray and Replace. I was thinking that I was just setting my data type wrong via Dim with "Formula". Not String, Not Variant. Wanted to know if a Data Type would haddle this.

  6. #6
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Convert String from Excel Range.Value to Equation in VBA

    I would continue to use String. One way to put the result back into the worksheet would be:

    Please Login or Register  to view this content.
    I like jindon's approach. The variables would be visible in both VBA and the worksheet.
    Last edited by Jakobshavn; 08-17-2013 at 03:39 PM.

  7. #7
    Forum Contributor
    Join Date
    08-14-2013
    Location
    Florida
    MS-Off Ver
    Excel 2013
    Posts
    105

    Re: Convert String from Excel Range.Value to Equation in VBA

    Quote Originally Posted by Norie View Post
    Where do you want to calculate this formula?
    The calculated formula is done in VBA several times (with the variables changing each time). I will then store each calculation in an Array (I know this part). At the end I export the Array with all of its data (I know this part too).

  8. #8
    Forum Contributor
    Join Date
    08-14-2013
    Location
    Florida
    MS-Off Ver
    Excel 2013
    Posts
    105

    Re: Convert String from Excel Range.Value to Equation in VBA

    Quote Originally Posted by jindon View Post
    Try
    Please Login or Register  to view this content.
    This is cool. I can add ".Names.Add "Pmat", "=" & ThisWorkbook.Sheets("Sheet1").Range("A1").Value" or even ".Names.Add "Pmat", "=" & any vba variables"
    I can put this in a function to give me my number, then store that number in my array.

    I can even do something like;

    Please Login or Register  to view this content.
    Last edited by Kalithro; 08-17-2013 at 03:55 PM.

+ 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. Convert string to range
    By ZacharyBass in forum Excel General
    Replies: 2
    Last Post: 11-26-2012, 12:58 AM
  2. Convert String to Row Range
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-03-2011, 04:07 PM
  3. Convert string to range
    By Cumberland in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-26-2006, 06:32 AM
  4. [SOLVED] convert a string to range?
    By JK in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-19-2006, 08:10 PM
  5. HOW TO CONVERT A FIGURE INTO TEXT (EQUATION USED IN EXCEL)
    By ASHOKAN NAMBIAR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-18-2005, 01:05 AM

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