+ Reply to Thread
Results 1 to 6 of 6

Excel VBA: programmatically insert PY( ) call in worksheet cell?

  1. #1
    Registered User
    Join Date
    10-17-2023
    Location
    Hong Kong
    MS-Off Ver
    365
    Posts
    3

    Excel VBA: programmatically insert PY( ) call in worksheet cell?

    Hi--In Excel, I need to programmatically insert a call to the interesting new PY( ) function in a worksheet cell, with return as an Excel value (not python object). Tried repeatedly using a few different ways, but all failed--see simple example below that shows how I tried, and the resulting errors).

    Note that manually creating a PY( ) call with exactly the same test python code works fine, so clearly I'm doing the "insert from VBA" wrong. Searched but did not find anything online on this. Anyone succeeded in doing this, please?

    Would be grateful to hear from anyone who's succeeded in doing this, or who can point me to any Microsoft guidance on this specific problem (I failed to find such).

    Thanks,
    Rob

    Details:
    ========
    Environment:
    Windows Prof 64bit, Office 365, Excel 32bit (Insider Beta ver 2311 build 16.0.17005.20000)

    Simple steps to recreate problem:
    - Create a new .xlsm workbook (single worksheet)
    - Create a VBA module, and define subroutine TestPyInsert() (see below)
    - Run this subroutine, wait for newly-created PY() call to complete,
    then save & close workbook
    - Re-open the workbook--Excel pops up 2 ~error messages:
    1) "We found a problem with some content in <mywkbk.xlsm>.
    Do you want us to try to recover as much as we can? ..."
    2) "Repairs to <mywkbk.xlsm>: Excel was able to open the file
    by repairing or removing the unreadable content.
    (...then lists 2 removed records--calc chain corruption?)
    Please Login or Register  to view this content.
    Last edited by hrob45; 10-19-2023 at 01:52 AM.

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Excel VBA: programmatically insert PY( ) call in worksheet cell?

    Hi hrob45, welcome to the forum.
    I don't have Office365, so I'm unable to experiment, but if Python code entries must be confirmed with CTRL+ENTER, I suspect VBA's normal Range.Formula construction won't work. I base this conclusion on the way VBA handles Array Formulas, which also require CTRL-sequence confirmation in older versions of Excel. These are assigned programmatically as Range.FormulaArray.

    Did you try recording a manual entry of your Py() formula as a macro?
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

  3. #3
    Registered User
    Join Date
    10-17-2023
    Location
    Hong Kong
    MS-Off Ver
    365
    Posts
    3

    Re: Excel VBA: programmatically insert PY( ) call in worksheet cell?

    Wonderfully sensible and simple idea of using the recorder to tease out the correct syntax...that did not occur to me to try--thank you! Tried and found the solution:

    It seems that to programmatically insert a =PY( ) call in a cell (see my prior post for my OS, Excel version, etc), what works for me is this VBA code:
    Please Login or Register  to view this content.
    Many thanks to leelnich for the prompt and valuable suggestion. Mods, assuming no one disagrees, please kindly mark inquiry as solved.
    Thanks again,
    Rob
    Last edited by hrob45; 10-19-2023 at 01:51 AM.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Excel VBA: programmatically insert PY( ) call in worksheet cell?

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Guideline #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  5. #5
    Registered User
    Join Date
    10-17-2023
    Location
    Hong Kong
    MS-Off Ver
    365
    Posts
    3

    Re: Excel VBA: programmatically insert PY( ) call in worksheet cell?

    Thanks--now done.

  6. #6
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Excel VBA: programmatically insert PY( ) call in worksheet cell?

    You're welcome, thanks for sharing your solution...
    NOTE : As the original poster/owner, only you can mark your thread as SOLVED (Thread Tools above Post # 1). - Lee
    Last edited by leelnich; 10-19-2023 at 02:11 AM.

+ 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. [SOLVED] Excel 2010 - VBA code to automatically insert username into cell on worksheet
    By theshybutterfly in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-28-2015, 07:58 PM
  2. Replies: 2
    Last Post: 12-13-2008, 05:58 PM
  3. Insert Trend function programmatically
    By mazatlanmexico in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-19-2007, 01:13 PM
  4. [SOLVED] VSTO and Excel-- End Cell Edit Mode Programmatically?
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-25-2006, 01:50 AM
  5. [SOLVED] Programmatically generate name of functition to call
    By Mel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-25-2005, 09:06 AM
  6. [SOLVED] how to programmatically display data into an excel worksheet
    By cEciLlE in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2005, 11:06 AM
  7. how do i programmatically insert a button on a worksheet
    By Qaspec in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2005, 02:06 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