how to change font color of excel using Matlab (at specific sheet)
27 views (last 30 days)
Show older comments
Hi:
from the answer in link below, I learned the way to change cell color of Excel sheet using Matlab, however it only tells how to change the cell color, at the 1st default sheet.
now I want change the font color, at specific sheet of a EXCEL file, is it possible to do this in Matlab?
Thanks!
Yu
0 Comments
Accepted Answer
Ravi Raja
on 27 Feb 2024
% Connect to Excel
excel = actxserver('Excel.Application');
excel.Visible = true; % Set to true if you want Excel to be visible
% Open the Excel file
workbook = excel.Workbooks.Open('C:\Users\raviraja\OneDrive\Documents\Book1.xlsx');
% Get the specific sheet
sheet = workbook.Sheets.Item('Sheet1'); % Replace 'SheetName' with the name of your sheet
% Define the cell range
cellRange = 'E1:E13'; % Change to the cell or range of cells you want to modify
% Get the Range object
range = sheet.Range(cellRange);
% Change font color to RGB value
red = 255; % Change these values according to the RGB color you desire
green = 0;
blue = 0;
rgbColor = red + (green * 256) + (blue * 256^2); % Convert RGB to Excel color value
range.Font.Color = rgbColor;
% Save the changes
workbook.Save;
% Close Excel
excel.Quit;
excel.delete;
In place of "'C:\Users\raviraja\OneDrive\Documents\Book1.xlsx'"
use your file
5 Comments
Ravi Raja
on 29 Feb 2024
addpath 'D:\'
excel = actxserver('Excel.Application');
% excel.Visible = true; % Set to true if you want Excel to be visible
% Open the Excel file
workbook = excel.Workbooks.Open('D:\Book1.xlsx');
% Get the specific sheet
sheet = workbook.Sheets.Item('Sheet1'); % Replace 'SheetName' with the name of your sheet
% Define the cell locations
cellLocations = {'A1', 'A7', 'B1'};
% Change font color to RGB value for each cell
red = 255; % Change these values according to the RGB color you desire
green = 0;
blue = 0;
rgbColor = red + (green * 256) + (blue * 256^2); % Convert RGB to Excel color value
for i = 1:length(cellLocations)
% Get the Range object for the current cell
range = sheet.Range(cellLocations{i});
% Set font color
range.Font.Color = rgbColor;
end
% Save the changes
workbook.Save;
% Close Excel
excel.Quit;
excel.delete;
More Answers (1)
Shreeya
on 27 Feb 2024
Edited: Shreeya
on 27 Feb 2024
From the question, I understand that you want to change the color of a cell in a specific sheet of the excel file. In the below code referenced from the linked answer,
WB.Worksheets.Item(1).Range('A1').Interior.ColorIndex = 3;
Item(1) refers to the first sheet in the excel file. You can change this number to index into the sheet you want to see the changes in.
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!