It may be possible to do this using VBA, but here I propose another solution that allows you to do this using JavaScript . You can use the free Excel add-in called Funfun, which allows you to use JavaScript code directly in Excel. At the same time, you can use libraries such as HighCharts.js or D3.js to draw a pie chart and manage the color separately. Here is an example that I made based on your description.

The colors of the different parts of the pie chart are determined by the data of the third column in the spreadsheet. In this example, I used HighCharts.js to control the drawing of this chart. The specific code that controls the color can be seen below.
var pieColors =[]; for(var i=1;i<data.length;i++){ switch(parseInt(data[i][2])){ case 0: pieColors.push('blue'); break; case 1: pieColors.push('green'); break; case 2: pieColors.push('orange'); break; case 3: pieColors.push('purple'); break; case 4: pieColors.push('red'); break; case 5: pieColors.push('brown'); break; } }
In this example, I just manually selected six colors, but you can easily generate a random color or color in a specific range using JavaScript code.
The Funfun add-in also has an online editor where you can test your code and result. You can check the detailed code and this example from the link below.
https://www.funfun.io/1/#/edit/5a4f4680c3a8a526caeec989
Once you are satisfied with your result, you can easily load the result into your Excel using the link above. But, of course, firstly, you need to add the Funfun add-in to your Excel using Insert - Office Add-Ins. Here are some screenshots showing how you load an example in Excel.


Disclosure: I'm a Funfun Developer
Chuan qin
source share