format cells

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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";
 
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?
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top