# How to point to proper worksheet in excel

**URL:** <https://www.ruby-forum.com/t/how-to-point-to-proper-worksheet-in-excel/142360>\
**Category:** Ruby\
**Created:** [July 7, 2008, 9:48am UTC](https://www.ruby-forum.com/t/how-to-point-to-proper-worksheet-in-excel/142360 "2008-07-07T09:48:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 7, 2008, 9:48am UTC](https://www.ruby-forum.com/t/how-to-point-to-proper-worksheet-in-excel/142360/1 "2008-07-07T09:48:03Z")

</div>

I have following code:

@xl = WIN32OLE.new(‘Excel.Application’)  
@xl[‘Visible’] = TRUE  
@wb = @xl.Workbooks.Add  
@ws = @xb.Worksheets(“XXXXX”)

The problem is that last line throws exception: moethod\_missing:  
Worksheets

Why? I had read examples from net and did everything according to them.

Another question is how to create a chart in excel and:  
add several series to it  
named it  
assign names to axis  
assign names to the series  
set chart type.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 7, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/how-to-point-to-proper-worksheet-in-excel/142360/2 "2008-07-07T14:33:53Z")

</div>

I have following code:

@xl = WIN32OLE.new(‘Excel.Application’)  
@xl[‘Visible’] = TRUE  
@wb = @xl.Workbooks.Add  
@ws = @xb.Worksheets(“XXXXX”)

Try this:

@wb.Sheets.Add#this adds a new ‘generic’ worksheet to your workbook  
(also note that you have a typo in the last line of the code above @xb  
 → @wb)  
new\_sheet = @wb.Worksheets(4)  
new\_sheet.Name = “XXXXX”

The problem is that last line throws exception: moethod\_missing:  
Worksheets

Why? I had read examples from net and did everything according to them.

If you poke around in irb you can get the OLE methods that you can use  
via a method called “ole\_methods”

## Another question is how to create a chart in excel and: add several series to it named it assign names to axis assign names to the series set chart type.

If you’re really set on using ruby, I’d build a macro in XL to do what  
you want and then work backwards from the macro-code to try and automate  
this process via ruby.  
Alternatively (and perhaps easier) is to simply build a workbook with an  
embedded macro to build a chart of your choosing it in the first place  
and use this as a starting point–you could even call the macro from  
ruby.  
Or if the data will always be in the same cells, you can pre-build the  
chart and simply use ruby (or whatever) to populate the relevant cells  
and the chart will already be there!  
There are many possibilities, but the best probably depends on the  
specifics of what you’re trying to do.

Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

This email and any attached files are confidential and intended solely  
for the intended recipient(s). If you are not the named recipient you  
should not read, distribute, copy or alter this email. Any views or  
opinions expressed in this email are those of the author and do not  
represent those of the company. Warning: Although precautions have been  
taken to make sure no viruses are present in this email, the company  
cannot accept responsibility for any loss or damage that arise from the  
use of this email or attachments.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 8, 2008, 4:06am UTC](https://www.ruby-forum.com/t/how-to-point-to-proper-worksheet-in-excel/142360/3 "2008-07-08T04:06:08Z")

</div>

On Jul 7, 2:44 am, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why? I had read examples from net and did everything according to them.
> 
> ## Another question is how to create a chart in excel and: add several series to it named it assign names to axis assign names to the series set chart type.
> 
> Posted viahttp://www.ruby-forum.com/.

Here’s an exzmple that may help

require ‘win32ole’  
excel = WIN32OLE::new(‘excel.Application’)  
excel[‘Visible’] = true  
workbook = excel.Workbooks.Open(filename)  
worksheet = workbook.Worksheets(1) #get hold of the first worksheet

You can also find a lot of information on using Ruby with Excel here

> **[Ruby on Windows](http://rubyonwindows.blogspot.com/)**
>
> On using the Ruby programming language on the Microsoft Windows platform

Hope this helps.

Luis

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [July 8, 2008, 12:30am UTC](https://www.ruby-forum.com/t/how-to-point-to-proper-worksheet-in-excel/142360/4 "2008-07-08T00:30:13Z")

</div>

Marcin T. wrote:

> I have following code:
> 
> @xl = WIN32OLE.new(‘Excel.Application’)  
> @xl[‘Visible’] = TRUE  
> @wb = @xl.Workbooks.Add  
> @ws = @xb.Worksheets(“XXXXX”)
> 
> The problem is that last line throws exception: moethod\_missing:  
> Worksheets
> 
> Why? I had read examples from net and did everything according to them.

As Paul mentioned, you had a typo, where “@xb” should be “@wb”.

A newly-created workbook (which you created via the Workbooks.Add  
method) will have the default worksheet names (ie, Sheet1", “Sheet2”)…

@ws = @wb.Worksheets(“Sheet1”)

You’ll find details about working with Excel’s workbook and worksheet  
objects, with Ruby, here:

> **[Automating Excel with Ruby: The Workbook Object](http://rubyonwindows.blogspot.com/2007/03/automating-excel-with-ruby-workbook.html)**
>
> Previously, we looked at the Excel Application object. Today, we'll take it down a level and investigate the Workbooks collection and Workbo...

> **[Automating Excel with Ruby: The Worksheet Object](http://rubyonwindows.blogspot.com/2007/04/automating-excel-with-ruby-worksheet.html)**
>
> Yesterday, we looked at the Excel Workbook object. Today, we'll work with the Worksheets collection and Worksheet objects. The Workbook obje...

> **[Ruby on Windows](http://rubyonwindows.blogspot.com/search/label/excel)**
>
> On using the Ruby programming language on the Microsoft Windows platform

> Another question is how to create a chart in excel and:  
> add several series to it  
> named it  
> assign names to axis  
> assign names to the series  
> set chart type.

See my reply to your other post here specifically about Excel charts.

David

> **[Ruby on Windows](http://rubyonwindows.blogspot.com)**
>
> On using the Ruby programming language on the Microsoft Windows platform
