The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

VBA - Read string from Excel cell and execute as VBA Code

Last post 08-20-2009 11:50 AM by gary.mcelroy. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 08-13-2009 8:29 PM

    VBA - Read string from Excel cell and execute as VBA Code

    OK, I've searched and been unable to find a solution for my problem.  I have VBA code that reads multiple Excel spreadsheets and outputs multiple CSV files for import into an accounting system.  The VBA code is fairly generic and is controlled by a mapping definition between the import spreadsheets and output CSV files.  Although I attempted to keep any calculations out of my code, it looks like this is going to be the best solution.  But I still want to keep the code generic and give the end users the ability to maintain via Excel.  I want to have a new column in the mapping that a "formula" can be entered and applied against the current field value.  The "formula" might be an arithmatic operation or a VLOOKUP/translation.  I'd like to basically put any Excel or VBA function in here and have it executed using "value" as my current field.  I tried EVALUATE, but I get Error 2029.  So, for example, I might put "value * -1" as the formula I want executed and maybe have a Range Name=CALC point to that formula.  In my VBA code, I would expect to be able to have "value = CALC".  I'm open to any and all suggestions.  Last minute changes on deliveries, so need responses ASAP.  Thanks in advance for any assistance.

    Gary

  • 08-14-2009 3:58 AM In reply to

    Re: VBA - Read string from Excel cell and execute as VBA Code

     Have you searched the error message?


    In complex analysis, a meromorphic function on an open subset D of the complex plane is a function that is holomorphic on all D except a set of isolated points

  • 08-14-2009 11:42 AM In reply to

    Re: VBA - Read string from Excel cell and execute as VBA Code

    Yes, the error message indicates a type mismatch and possibly that the EVALUATE only works reading Excel cells whereas the "value" field only exists in the VBA code.  So, one possible solution is to write the "value" field to a cell and then attempt the evaluate; however, I didn't find this as a solution in my research so was looking for advice before wasting more time.

    Gary

  • 08-19-2009 8:42 PM In reply to

    • Nfol
    • Not Ranked
    • Joined on 12-22-2005
    • Posts 4

    Re: VBA - Read string from Excel cell and execute as VBA Code

    I'm not completely sure what you are aiming for, and it is vague as to what was tried already, but I have an idea that is a bit crude. Make the input "formula" be like a normal Excel formula (it may be better to treat it as plain text) but have something like VALUE (or VALUE1, VALUE2, etc.) for where the variable(s) would go. In the VBA code, do a string replacement of all instances of VALUE (or VALUE1, VALUE2, etc.) into the actual value to evaluate. Probably Range.Formula and Evaluate would help in this.
    --==[ Nfol ]==--
  • 08-20-2009 11:50 AM In reply to

    Re: VBA - Read string from Excel cell and execute as VBA Code

    Thanks for the help.  I've resolved my initial issues.  Based on the Google research, I got the impression that Evaluate only works with cell references and will not work with VBA fields.  So, I updated a temp cell with my initial value from the "value" field, then used the EVALUATE reading my original formula Text cell.  So, final answer is:

    1. Formula Cell = sum(Temp!$AA$1 * -1)
        This has the reference to my temporary holding cell AA1 on TEMP worksheet.
    2. VBA Code = 
                    ThisWorkbook.Worksheets("Temp").Activate
                    ThisWorkbook.Worksheets("Temp").Range("AA1").Activate
                    ActiveCell.value = value
                    value = Evaluate(FldCalc(cnt))
       FldCalc is a Collection where I'm storing the text formulas.

    What I'm hoping now, is that I can put almost any Excel or VBA formula into my Formula column and have it execute.  But, I expect to have to tweek it as I go. 

    Again, thanks for the input.

    Gary

Page 1 of 1 (5 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems