# Using Rhino with Excel VBA

**URL:** <https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334>\
**Category:** Scripting\
**Tags:** windows, rhino7\
**Created:** [October 19, 2022, 3:56pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334 "2022-10-19T15:56:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Celal\_KIRANDAG](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/celal_kirandag/32/674735_2.png) [@Celal\_KIRANDAG](https://discourse.mcneel.com/u/Celal_KIRANDAG)\
**Post date:** [October 19, 2022, 3:56pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/1 "2022-10-19T15:56:58Z")

</div>

Hello Everyone,  
In the past, I used Excel VBA to do lots of calculations and automated workbooks for certain routine operations.  
One of them was to create a 3D model in Microstation ( Bentley CAD Program) using Excel VBA program.

I was thinking about how i can do the same for Rhino.

Does anyone have any experience in this?

Thanks for your time

regards

CK

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [October 19, 2022, 5:48pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/2 "2022-10-19T17:48:00Z")

</div>

Hi,  
The closest thing to VBA perhaps would be RhinoScript

> **[RhinoScript](https://developer.rhino3d.com/api/rhinoscript/)**
>
> WebHelp 5.50

Alternative languages for scripting in Rhino is Python or C#, which also are supported by lots of Math-libraries out there.

//Rolf

---

<div class="post-metadata">

**Author:** ![Gijs](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/gijs/32/769882_2.png) [@Gijs](https://discourse.mcneel.com/u/Gijs)\
**Post date:** [October 19, 2022, 5:54pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/3 "2022-10-19T17:54:14Z")

</div>

I have some experience connecting Rhino to excel worksheets with Python. It’s not completely clear what your expectations are regarding the exchange between the two.

---

<div class="post-metadata">

**Author:** ![davidcockey](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/davidcockey/32/771921_2.png) [@davidcockey](https://discourse.mcneel.com/u/davidcockey)\
**Post date:** [October 19, 2022, 6:17pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/4 "2022-10-19T18:17:39Z")

</div>

This thread may be relevant: [Connect to Rhino from Excel - #14 by eric.bunn](https://discourse.mcneel.com/t/connect-to-rhino-from-excel/93663/14)

---

<div class="post-metadata">

**Author:** ![Celal\_KIRANDAG](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/celal_kirandag/32/674735_2.png) [@Celal\_KIRANDAG](https://discourse.mcneel.com/u/Celal_KIRANDAG)\
**Post date:** [October 19, 2022, 6:37pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/5 "2022-10-19T18:37:02Z")

</div>

Hello Gijs,  
In Excel VBA, typically i had an interface to define my parameters and i take these parameters and calculate the coordinates of the intended model and create the object using the applicable syntax of the specific program.  
i am aiming to do the same for rhino, using excel vba.

i saw someone posted this message here and it said that Rhino script can be used in excel vba.

That s why i was asking this question

let me know

thanks

CK

---

<div class="post-metadata">

**Author:** ![benedict](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/benedict/32/535424_2.png) [@benedict](https://discourse.mcneel.com/u/benedict)\
**Post date:** [October 19, 2022, 6:51pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/6 "2022-10-19T18:51:11Z")

</div>

well, here you got an example node on how to connect rhino with excel via vb node. Please note that screenupdate switched in between. In case your code fails, Excel won’t update the screen any more. You can comment out these lines.

 ![Unbenannt-1](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/3/7/0/37058ecda194ecdd05073ab2f0758b63d7d1d17a.jpeg)  
here’s the file:

[vba\_base.gh](https://discourse.mcneel.com/uploads/short-url/4Bc0uoj1Pn7Tca6xmFipkwI9pDG.gh) (3.4 KB)

EDIT: this uses grasshopper, which is part of rhino, as @davidcockey pointed out correctly. I think using grasshopper allows you to easily get values from (and write to) Excel and make further operations with the values or the created geometry.  
It should also be noted that it is not a good idea to connect a toggle to the node without a timer. This usually makes crash xls.

hope that helps

---

<div class="post-metadata">

**Author:** ![Gijs](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/gijs/32/769882_2.png) [@Gijs](https://discourse.mcneel.com/u/Gijs)\
**Post date:** [October 19, 2022, 7:20pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/7 "2022-10-19T19:20:03Z")

</div>

I see, I have no experience with Excel VBA, but since @benedict already posted an example, I guess you’re covered. If you need something in python then let me know. What I have is only reading stuff from excel though and some code to handle correct closing of opened excel files (which was far from straightforward, hence I mention)

---

<div class="post-metadata">

**Author:** ![davidcockey](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/davidcockey/32/771921_2.png) [@davidcockey](https://discourse.mcneel.com/u/davidcockey)\
**Post date:** [October 19, 2022, 7:27pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/8 "2022-10-19T19:27:24Z")

</div>

> [@benedict](#):
>
> well, here you got an example node on how to connect rhino with excel via vb node. Please note that screenupdate switched in between. In case your code fails, Excel won’t update the screen any more. You can comment out these lines.
> 
> ![Unbenannt-1](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/3/7/0/37058ecda194ecdd05073ab2f0758b63d7d1d17a.jpeg)

It should be noted that this uses Grasshopper, which is part of Rhino.

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [October 19, 2022, 7:35pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/9 "2022-10-19T19:35:13Z")

</div>

**Edit** : Ops, I thought the code was in VBA, so the code below doesn’t work. In [VB.NET](http://VB.NET) you could use Try… Finally … End Try instead,

```nohighlight
Try 
    ' All your code here
Finally
    objExcel.screenUpdating = True
End Try

```

VBA below only works in Excel:

@benedict , you can ensure restoring Screenupdate by wrapping your code in an exception handle, like so:

VBA:

```auto
Private Sub RunScript(ByVal _stream... )

    objExcel.screenUpdating = False
    On Error GoTo OnException ' Wrap code in exception handler
        If _stream Then
            ' ...
            ' ...
            ' ...
            objExcel.screenUpdating = True
        End If
    Exit Sub ' If no exception, the sub ends here

OnException:
    objExcel.screenUpdating = True ' <-- Screen update restored
End Sub

```

//Rolf

---

<div class="post-metadata">

**Author:** ![benedict](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/benedict/32/535424_2.png) [@benedict](https://discourse.mcneel.com/u/benedict)\
**Post date:** [October 19, 2022, 7:38pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/10 "2022-10-19T19:38:34Z")

</div>

yeah, it’s pretty brute what I’ve posted here 🙂 I’m more into c# although it’s about the same thing.  
thanks @RIL!!

it is VBA, all you did is correct

EDIT: ah, yeah, I always mess up with vba and [vb.Net](http://vb.Net) as well 🙂 goto should work with vba try…except was c# and [vb.net](http://vb.net)

thank you in any case, I usually don’t update old nodes, but I guess remembering how much trouble it had made back then, I probably should

---

<div class="post-metadata">

**Author:** ![Celal\_KIRANDAG](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/celal_kirandag/32/674735_2.png) [@Celal\_KIRANDAG](https://discourse.mcneel.com/u/Celal_KIRANDAG)\
**Post date:** [November 1, 2022, 9:05pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/11 "2022-11-01T21:05:06Z")

</div>

Thank you very much Ben  
I really appreciate it

---

<div class="post-metadata">

**Author:** ![Celal\_KIRANDAG](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/celal_kirandag/32/674735_2.png) [@Celal\_KIRANDAG](https://discourse.mcneel.com/u/Celal_KIRANDAG)\
**Post date:** [January 10, 2023, 11:20pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/12 "2023-01-10T23:20:23Z")

</div>

Hello Gijs,  
As i was digging in Rhino, i realized that i could do the same thing that i was planning to do using excel vba by Python script within Rhino.  
I an interested to know that how you can read data from excel within Python code.  
Could you send me an example file that uses that way of getting data into Python code?  
Thanks  
CK

---

<div class="post-metadata">

**Author:** ![farouk.serragedine](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/farouk.serragedine/32/770975_2.png) [@farouk.serragedine](https://discourse.mcneel.com/u/farouk.serragedine)\
**Post date:** [January 11, 2023, 12:58pm UTC](https://discourse.mcneel.com/t/using-rhino-with-excel-vba/149334/13 "2023-01-11T12:58:41Z")

</div>

IronPython 2.7 used by Rhino is an implementation of Python that can run on the .NET Framework, therefore it provides support for interacting with Excel using the Microsoft Office Interop libraries.

You can use the `pywin32` library to interact with Excel from IronPython. Here’s an example of how you can read data from an Excel spreadsheet using IronPython 2.7  
It should be very easy and straightforward

```auto
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
from Microsoft.Office.Interop import Excel

xlApp = Excel.ApplicationClass()
xlApp.Visible = False

xlWorkbook = xlApp.Workbooks.Open(r"C:\path\to\your\excel\file.xlsx")
xlWorksheet = xlWorkbook.Sheets[1]

for row in range(1, xlWorksheet.UsedRange.Rows.Count + 1):
    for col in range(1, xlWorksheet.UsedRange.Columns.Count + 1):
        cell = xlWorksheet.Cells[row, col]
        print(cell.Value)

xlWorkbook.Close()
xlApp.Quit()

```
