Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change Excel column width

I'm handling an excel file using visual studio 2013, visual basic, windows Forms.

  • I add new sheet to excel named sheetReport

How to change the column width of b to be 30?

I don't need to use autofit, cause it make text small to fit in cell with it's default width.

xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
xlBook = xlApp.Workbooks.Open(FileName)
SheetReport = CType(xlBook.Sheets.Add(), Excel.Worksheet)
SheetReport.Name = "Report"
SheetReport.Range("B2").Value = "Agent Name"
' need resize column b
like image 500
E R Avatar asked Apr 17 '14 07:04

E R


People also ask

How do I quickly resize a column in Excel?

Left-click the mouse button in the header between the columns or rows that you selected and drag the mouse to the left and right for columns and up and down for the rows to adjust the size of all of the selected columns at once. That's it.

How do I change the width of multiple columns in Excel?

To increase the width of multiple consecutive columns, select the columns you want to change. Click the header boundary of any selected column and drag to the right, like in the picture below. To decrease the width, drag to the left.


1 Answers

Simply:

SheetReport.Range("B2").ColumnWidth = 30

To learn the code with Excel API I'll give you a trick you can use; just open a Excel Sheet, start recording a Macro, and then do the thing you want to do programatically. Then stop the recording and look for the code recorded of the macro.

It will contain the instructions in VBA to make it possible. With a little modification, that's what you need to do in VB.NET with the Excel API.

like image 123
SysDragon Avatar answered Oct 02 '22 13:10

SysDragon