how to change Excel column width

26,018

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.

Share:
26,018

Related videos on Youtube

E R
Author by

E R

Just Ehab Ragab

Updated on April 17, 2020

Comments

  • E R
    E R about 4 years

    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
    
    • Dmitry Pavliv
      Dmitry Pavliv about 10 years
      SheetReport.Range("B2").EntireColumn.ColumnWidth = 30?

Related