Setting cell value using button on the ribbon

163 views
Skip to first unread message

Steven John Lally

unread,
Aug 2, 2016, 5:41:38 AM8/2/16
to Excel-DNA
I have a custom ribbon with a button on it.

I want to be able to select a cell and click that button so that it does something to the selected cell.

For example, set it's value to 100 or to "Hello".


Govert van Drimmelen

unread,
Aug 2, 2016, 5:50:47 AM8/2/16
to exce...@googlegroups.com

Hi Steven,

 

1.       Add a reference to the interop assembly Microsoft.Office.Interop.Excel (if you haven’t got it already).

2.       Get the root Application object with a call to ExcelDnaUtil.Application:

 

var xlApp = (Microsoft.Office.Interop.Excel)ExcelDnaUtil.Application;

 

3.       Proceed as in VBA:

 

xlApp.ActiveCell.Value = "Hello";

 

-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 post to this group, send email to exce...@googlegroups.com.
Visit this group at https://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

Govert van Drimmelen

unread,
Aug 2, 2016, 5:59:21 AM8/2/16
to exce...@googlegroups.com

Oops – that should be:

 

var xlApp = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application;

 

 

(Of course you can simplify it with a ‘using’ statement…)

Steven John Lally

unread,
Aug 2, 2016, 6:03:02 AM8/2/16
to Excel-DNA
Hello Govert,

Thank you so much, it works.

Turned out to be much simpler than I thought. I don't know why I wasn't able to really find an answer elsewhere.

Steven John Lally

unread,
Aug 2, 2016, 6:13:31 AM8/2/16
to Excel-DNA
Sorry, one more thing.

Is it possible instead of setting it to the active cell, it can set it to a specified cell in the code?

Regards,
Steven


On Tuesday, 2 August 2016 17:41:38 UTC+8, Steven John Lally wrote:

Govert van Drimmelen

unread,
Aug 2, 2016, 6:29:20 AM8/2/16
to exce...@googlegroups.com

In that context the COM object model is the same as you have in VBA:

 

xlApp.Range["D3"].Value = "Hello";

xlApp.Range["Sheet1!D3"].Value = "Hello";

xlApp.ActiveSheet.Cells(3, 4).Value = "Hello";

 

-Govert

 

From: exce...@googlegroups.com [mailto:exce...@googlegroups.com] On Behalf Of Steven John Lally


Sent: 2 August 2016 12:14
To: Excel-DNA <exce...@googlegroups.com>

--

Steven John Lally

unread,
Aug 2, 2016, 10:59:50 PM8/2/16
to Excel-DNA
Thank you so much for your help, Govert. It's good to see a developer like you, who I assume to be quite busy, take your time out to answer even the simplest question.

Once again, thanks a lot.

Best Regards,
Steven

Govert van Drimmelen

unread,
Aug 3, 2016, 2:55:39 AM8/3/16
to exce...@googlegroups.com
Thank you for the kind words, Steven.

I gratefully accept donations via PayPal:

Regards,
Govert



From: exce...@googlegroups.com [exce...@googlegroups.com] on behalf of Steven John Lally [stevenj...@gmail.com]
Sent: 03 August 2016 04:59 AM
To: Excel-DNA
Subject: Re: [ExcelDna] Re: Setting cell value using button on the ribbon

--
Reply all
Reply to author
Forward
0 new messages