# For loop in excel macro

**URL:** <https://www.ruby-forum.com/t/for-loop-in-excel-macro/177048>\
**Category:** Ruby\
**Created:** [October 10, 2009, 1:56pm UTC](https://www.ruby-forum.com/t/for-loop-in-excel-macro/177048 "2009-10-10T13:56:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 10, 2009, 1:56pm UTC](https://www.ruby-forum.com/t/for-loop-in-excel-macro/177048/1 "2009-10-10T13:56:31Z")

</div>

Hi all,

Here is an off -Ruby topic. Recently I need to do a lot of work in Excel  
with repeated work. And macro might be some help.  
I wonder how I can loop some ranges of cells using a for loop in  
Excel.

Thanks,

Li

For i =0 To 4 Step 1  
ActiveSheet.Shapes.AddChart.Select  
ActiveChart.ChartType = xlColumnClustered  
ActiveChart.SetSourceData Source:=Range(“A1:D1,A2:D2”)

```
MsgBox i
Next

```

###############################  
after each step the following line should change as follows:

Range(“A1:D1,A2:D2”)

Range(“A1:D1,A3:D3”)

Range(“A1:D1,A4:D4”)

…

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 10, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/for-loop-in-excel-macro/177048/2 "2009-10-10T18:31:43Z")

</div>

Li Chen wrote:

> Hi all,
> 
> Here is an off -Ruby topic. Recently I need to do a lot of work in Excel  
> with repeated work. And macro might be some help.  
> I wonder how I can loop some ranges of cells using a for loop in  
> Excel.
> 
> Thanks,
> 
> Li
> 
> For i =0 To 4 Step 1  
> ActiveSheet.Shapes.AddChart.Select  
> ActiveChart.ChartType = xlColumnClustered  
> ActiveChart.SetSourceData Source:=Range(“A1:D1,A2:D2”)
> 
> ```
> MsgBox i
> Next
> 
> ```
> 
> ###############################  
> after each step the following line should change as follows:
> 
> Range(“A1:D1,A2:D2”)
> 
> Range(“A1:D1,A3:D3”)
> 
> Range(“A1:D1,A4:D4”)
> 
> …

You can perform this task using Ruby.  
You can find plenty of info about right here:

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

Then the required loop can be implemented in the ruby way you are used  
to.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 11, 2009, 12:41am UTC](https://www.ruby-forum.com/t/for-loop-in-excel-macro/177048/3 "2009-10-11T00:41:45Z")

</div>

Rodrigo B. wrote:

> You can perform this task using Ruby.  
> You can find plenty of info about right here:
> 
> [Ruby on Windows: excel](http://rubyonwindows.blogspot.com/search/label/excel)
> 
> Then the required loop can be implemented in the ruby way you are used  
> to.

Hi Rodrigo,

Thanks for the link. I might refer it if I use a lot of Excel  
automation. But I need a quick method for my project immediately. I  
just get some help from VBA group in Yahoo:

Range(“A1:D1,A” & i + 2 & “:D” & i + 2)

Li
