A more sophisticated way is to create a project variable containing the expression used to assign colors to the different values of the code
attribute. This is especially handy if you have a lot of layers to which you want to apply the same color scheme as you can make changes (e.g. defining another color ramp) once in the variable and all layers at once will automatically change.
First proceed as explained in my other solution, using assistant. When done, go to data driven override and select Edit...
. Copy the expression you find there. It should look something like:
coalesce(
ramp_color(
'Reds', -- change the name or definition of the color ramp here
scale_linear(
case
when code = 'abc123' then 1
when code = 'def243' then 2
when code = 'ghi653' then 3
end,
1,
3,
0,
1
)
),
'#000000'
)
Copy this expression, then go to Menu Project > Properties > Variables Tab
and add a new variable, here named color
. For the value of this variable, paste the expression from above.
Now you can set the same color scheme for every layer you want. Click data driven override for the color > Edit and paste the expression eval(@color)
, where color
is the name of the variable defined before:

