locked
How do You Change the width of a Excel cell or columns in VB.Net RRS feed

  • Question

  • I am having problems trying to change the width of a cell or even columns programatically in VB.Net. I have created an excel object and I can get data into excel, but I can't seem to find the right way to format the cells and columns. Here is what I have thus far.

    Dim xlApp As Microsoft.Office.Interop.Excel.Application

    Dim xlBook As Microsoft.Office.Interop.Excel.Workbook

    Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet

    Dim lb As Integer

    xlApp = CreateObject("Excel.Application")

    xlBook = xlApp.Workbooks.Add()

    xlSheet = xlBook.Worksheets(1)

    For lb = 0 To ListBox1.Items.Count - 1 Step 1

    Dim send As String

    Dim sendT As String

    Dim sendTE As String

    send = ListBox1.Items.Item(lb)

    sendT = ListBox2.Items.Item(lb)

    sendTE = ListBox3.Items.Item(lb)

    If lb >= 0 Then

    xlSheet.Cells.Item((lb + 1), 1) = send

    xlSheet.Cells.Item((lb + 1), 2) = sendT

    xlSheet.Cells.Item((lb + 1), 3) = sendTE

    End If

    Next

    xlBook.SaveAs("c:\Timesheet.xls")

    xlBook.Close()

    xlApp = Nothing

    How would I go about changing all the columns width in the sheet to a set value?

    Please help. 

    Friday, March 10, 2006 1:30 AM

Answers


  • The following worked for me:

    ActiveSheet.Columns("A:B").ColumnWidth = 13

    The unit of measurement is points (as in the font being used).

     

    Friday, March 10, 2006 2:49 PM