Create a macro, that changes the background color of columns

Hello, this is my first question and first post in this community.

I hope to find a hint, so I can understand what I am doing wrong so far.
I wish to change the background colors of several columns in a document - and since it is a task that I have to do quite often, I want to create and run a macro.

I can create a macro, I can save it - but I can not run it properly. The change of background colors that I recorded is run in an ill way.

I recorded different colors for different columns - and it is run in a way that all the colors I recorded, will splash all over the sheet one after the other - until I end up with the sheet entirely tinted in the color I recorded last.

I marked the columns properly.
What can I do to solve my problem?

Grateful for hints,

Ella_Cologne

Even if you insist in doing your “repetitive task” with the help of a macro, you should try to avoid the beginner’s mistake to apply “hard formatting”, and to use properly defined cell styles instead.

Anyway:
If you want to do something for all the columns intersecting with the current selection, you won’t get happy with a recorded macro. The macro recorder isn’t an “interaction logger”.
Nonetheless a recorded macro I created experimentally did what I supposed to be what you wanted: “Set the predefined color code for the current selection!”
Didn’t you select the respective columns actually in advance of every single macro call?

How, exactly did you do that? What exatly do you mean by “for different coluimns”?

Use templates and styles. Templates and styles are the essence of an office application since the 80ies.

1 Like

Thank you. That does not answer my question, though. I am talking about a repetitive task.

Templates and styles avoid repetitive tasks. You open a template and everything is formatted and layouted for data entry. You apply a cell style to expand your custom settings for cell ranges.
Templates and styles are the key to efficiency with Word, Excel, LibreOffice etc.

The best way to achieve this is to create one (or more, per color One) new cell styles.

Right-click on the cell style “Default” and choose New....
In the dialog “Cell style” enter a name in the tab “Organize”.
In the “Background” tab, select the button Colour, choose the colour, then click OK.

Now select the cells that should get a different colour.
Double-click on the new cell style, done.

If it was the wrong cells, select them again and double-click on the “Default” cell style.

See also:
Cell styles with different backgrounds