ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   format cells (https://www.excelbanter.com/excel-programming/373108-format-cells.html)

Pamish

format cells
 
I have this code below, in which I am trying to format cells with different
formats, is there a way of shortening this and maybe the change will make it
run faster.
These are sub heading that are suppose to be printed per produt info.

Range rg = oWorkSheet.get_Range("A" + LastRow.ToString(), "I" +
LastRow.ToString());

rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 9;
rg.Cells.RowHeight = 30;

rg = oWorkSheet.get_Range("A" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.Value2 = "string0";
LastRow++;
LastRow++;

rg = oWorkSheet.get_Range("A" + LastRow.ToString(), "I" +
LastRow.ToString());
rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 9;
rg.Cells.RowHeight = 30;

rg = oWorkSheet.get_Range("A" + LastRow.ToString(), Type.Missing);
rg.WrapText = true;
rg.Value2 = "string1";

rg = oWorkSheet.get_Range("B" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.ColumnWidth = 35;
rg.Value2 = "string2";

rg = oWorkSheet.get_Range("C" + LastRow.ToString(), Type.Missing);
rg.ColumnWidth = 35;
rg.WrapText = false;
rg.Value2 = "string3";

rg = oWorkSheet.get_Range("D" + LastRow.ToString(), Type.Missing);
rg.WrapText = true;
rg.Value2 = "string4";

rg = oWorkSheet.get_Range("E" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.Value2 = "string5";

rg = oWorkSheet.get_Range("F" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.ColumnWidth = 11;
rg.Value2 = "string6";

rg = oWorkSheet.get_Range("G" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.ColumnWidth = 9;
rg.Value2 = "string7";

ADG

format cells
 
Hi Pamish

Can only suggest using a with statement to speed execution once you have set
the range. e.g.

with rg
.Font.Bold = true;
.Font.Name = "Arial";
.Font.Size = 9;
.Cells.RowHeight = 30;
end with

P.S. What does LastRow++ do?
--
Tony Green


"Pamish" wrote:

I have this code below, in which I am trying to format cells with different
formats, is there a way of shortening this and maybe the change will make it
run faster.
These are sub heading that are suppose to be printed per produt info.

Range rg = oWorkSheet.get_Range("A" + LastRow.ToString(), "I" +
LastRow.ToString());

rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 9;
rg.Cells.RowHeight = 30;

rg = oWorkSheet.get_Range("A" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.Value2 = "string0";
LastRow++;
LastRow++;

rg = oWorkSheet.get_Range("A" + LastRow.ToString(), "I" +
LastRow.ToString());
rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 9;
rg.Cells.RowHeight = 30;

rg = oWorkSheet.get_Range("A" + LastRow.ToString(), Type.Missing);
rg.WrapText = true;
rg.Value2 = "string1";

rg = oWorkSheet.get_Range("B" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.ColumnWidth = 35;
rg.Value2 = "string2";

rg = oWorkSheet.get_Range("C" + LastRow.ToString(), Type.Missing);
rg.ColumnWidth = 35;
rg.WrapText = false;
rg.Value2 = "string3";

rg = oWorkSheet.get_Range("D" + LastRow.ToString(), Type.Missing);
rg.WrapText = true;
rg.Value2 = "string4";

rg = oWorkSheet.get_Range("E" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.Value2 = "string5";

rg = oWorkSheet.get_Range("F" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.ColumnWidth = 11;
rg.Value2 = "string6";

rg = oWorkSheet.get_Range("G" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.ColumnWidth = 9;
rg.Value2 = "string7";


Pamish

format cells
 
I am writing the program in c#, using Excel application to run my reports,
LastRow++ is telling it to go to the next row i.e if LastRow = 4, then after
LastRow++, LastRow will be 5.
The cells have got different formats ie. columnwidth, size, fontsize, etc.

"ADG" wrote:

Hi Pamish

Can only suggest using a with statement to speed execution once you have set
the range. e.g.

with rg
.Font.Bold = true;
.Font.Name = "Arial";
.Font.Size = 9;
.Cells.RowHeight = 30;
end with

P.S. What does LastRow++ do?
--
Tony Green


"Pamish" wrote:

I have this code below, in which I am trying to format cells with different
formats, is there a way of shortening this and maybe the change will make it
run faster.
These are sub heading that are suppose to be printed per produt info.

Range rg = oWorkSheet.get_Range("A" + LastRow.ToString(), "I" +
LastRow.ToString());

rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 9;
rg.Cells.RowHeight = 30;

rg = oWorkSheet.get_Range("A" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.Value2 = "string0";
LastRow++;
LastRow++;

rg = oWorkSheet.get_Range("A" + LastRow.ToString(), "I" +
LastRow.ToString());
rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 9;
rg.Cells.RowHeight = 30;

rg = oWorkSheet.get_Range("A" + LastRow.ToString(), Type.Missing);
rg.WrapText = true;
rg.Value2 = "string1";

rg = oWorkSheet.get_Range("B" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.ColumnWidth = 35;
rg.Value2 = "string2";

rg = oWorkSheet.get_Range("C" + LastRow.ToString(), Type.Missing);
rg.ColumnWidth = 35;
rg.WrapText = false;
rg.Value2 = "string3";

rg = oWorkSheet.get_Range("D" + LastRow.ToString(), Type.Missing);
rg.WrapText = true;
rg.Value2 = "string4";

rg = oWorkSheet.get_Range("E" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.Value2 = "string5";

rg = oWorkSheet.get_Range("F" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.ColumnWidth = 11;
rg.Value2 = "string6";

rg = oWorkSheet.get_Range("G" + LastRow.ToString(), Type.Missing);
rg.WrapText = false;
rg.ColumnWidth = 9;
rg.Value2 = "string7";



All times are GMT +1. The time now is 12:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com