Cannot write cell values

72 views
Skip to first unread message

Markus Mayer

unread,
Jan 29, 2022, 10:45:59 PM1/29/22
to Excel-DNA
Hi,


  Private Sub asdf()

    Dim wb As Workbook = Xl.ActiveWorkbook
    Dim sht As Worksheet = Xl.ActiveSheet


    Debug.Print(sht.Cells(3, 3).value2)

    sht.Cells(3, 3).value2 = 11

  End Sub


The debug.print successfully retreives the current cell value.
But I cannot assign a new value to it. The debugger exits the function at this point without halting on the exception. In the output window a "System.Runtime.InteropServices.COMException" in Microsoft.VisualBasic.dll" exception is thrown.

Am I not seeing the obvious?

Govert van Drimmelen

unread,
Jan 30, 2022, 12:52:27 AM1/30/22
to exce...@googlegroups.com

Hi Markus,

 

You are you calling the Sub?

 

If you’re calling form a Ribbon button or shortcut key, it should work fine.

If you’re calling if from inside a function from a cell formula, it won’t work because Excel does not allow you to write to the sheet while calculating.

Your function can just return a result directly.

 

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/a086514c-f22b-45b5-a4bd-9a0e8fbd2adan%40googlegroups.com.

Markus Mayer

unread,
Jan 30, 2022, 1:09:25 PM1/30/22
to Excel-DNA
Thanks for your hint, yes I called it from inside a formula. In one of your other posts I found the solution for my case. I have to queue my function to wait until Excel is ready and any user interaction has finished:

ExcelAsyncUtil.QueueAsMacro(Sub() asdf())
Reply all
Reply to author
Forward
0 new messages