+ Reply to Thread
Results 1 to 8 of 8

Convert imported text to a usable function in VBA

  1. #1
    Registered User
    Join Date
    11-07-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    8

    Convert imported text to a usable function in VBA

    Hey guys,

    I would like to be able to import a function as text (i.e. "x+2") and then convert that to a command in VBA so that I can use it as part of a calculation:

    if A1 contains "x+2" I would have the code be

    x=3
    .
    .
    .
    y=range("A1")
    .
    .
    .

    and have y be equal to 5. I dont know if this is even possible, but I would appreciate any input.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Convert imported text to a usable function in VBA

    Hi

    Bit contrived, but

    Please Login or Register  to view this content.
    rylo

  3. #3
    Registered User
    Join Date
    11-07-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Convert imported text to a usable function in VBA

    Thanks for getting back so quickly! That code works for sub procedures, but for some reason when I incorporate it in a function procedure it kicks me out of the code at the line:

    Application.Names.Add Name:="x", RefersTo:=x

    Any reason why this would be the case?

    Code for Function:

    Please Login or Register  to view this content.
    Last edited by Chick3nD3m0n; 11-08-2012 at 02:37 AM.

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Convert imported text to a usable function in VBA

    Hi

    You are trying to use a function to perform an action, whereas it should only be used to return a result. Things like adding names, naming sheets, inserting a value into a cell are all things that should be done by a sub, not a function.

    Try this.

    Please Login or Register  to view this content.
    It is pretty specific in that it has to know where the formula is, and what is to be replaced with the relevant value.
    Also, can you go back and edit your post and put your code inside code tags.


    rylo
    Last edited by rylo; 11-08-2012 at 01:14 AM.

  5. #5
    Registered User
    Join Date
    11-07-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Convert imported text to a usable function in VBA

    That worked, I think I can work with that for the time being. The only issue (minor) is that the cell with aaa doesnt automatically update when a new function is typed in cell A1. I think that is an issue with vba reiterating at the function at a certain time though. Its not a big deal really, thanks for your help!

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Convert imported text to a usable function in VBA

    Hi

    Put
    Please Login or Register  to view this content.
    into the function. This means that it will recalculate every time there is a change in the workbook. ALternatively, put in a second variable that will take the data from A1. If this cell is in the function, then it will update every time there is a change in that cell.

    rylo

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,373

    Re: Convert imported text to a usable function in VBA

    the other problem you are going to run into as written is that the "range("a1") object is unqualified, so it will always reference the active sheet. So if you have two tabs in a sheet or have multiple workbooks open, it will pull the wrong information when those other sheets are active.

    Better than an unqualified object and application.volatile is to pass the necessary information to the function through the argument list:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-07-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Convert imported text to a usable function in VBA

    I did both those things and now it works perfectly. Thanks again for all your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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