Sunday, September 22, 2013

How to change the background color of a cell in VBA? (VBA-EXCEL)

The easiest way to change the background color of a cell in VBA is using the color index, so if we want to change the color of cell A1 to red we should write:

Sub change_color()

Activesheet.range("a1").interior.colorindex=3

End sub

I leave here to you the color palette with the respective numbers of the colors, in case you want to color a cell but not with red :)


Thursday, September 19, 2013

How to change the chart type of just one of the series on a chart.-VBA(excel)

Sometimes we have a chart with different variables, if we want the different variables to appear on the same chart but with a different chart type we can do it on VBA:

Just select the chart, write this code and run it:

Sub Chart()

ActiveChart.ChartType =xlXYScatterSmooth
ActiveChart.SeriesCollection(1).ChartType = xlXYScatterLines

End Sub

In this example, we have as result a line to show the evolution of the variable 1, and just points to show the behaviour of the rest of the series. Easy, isn't it?

Tuesday, September 3, 2013

Set a Range (VBA-Excel)

One of the basic steps to solve a lot of problems on Excel is to set a range in order to use it and reuse it as many times as you like during a procedure (Macro).

Sub SetaRange()

Dim Rng as Range

Set Rng=Range("A1:C10")

End Sub

With the code above you set a range named Rng (you can choose a different name if you prefer), consisting of all the cells from A1 to C10 on the Active Sheet.