Link to home
Start Free TrialLog in
Avatar of tobin46
tobin46

asked on

Set Excel Column Width using VB.net

Hi!

I'm writing data to excel worksheet, but before I do, I want to format the sheet.

I've read many things online, forums, msdn, etc. and haven't found anything that provides an example of how to format a column's width.  

Does anyone have a code example?

Thanks!  
Avatar of Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Flag of New Zealand image

Hello,

in Excel VBA, the syntax would be something like

    Columns("G:G").ColumnWidth = 10.4

Don't know about .NET. The object is specified in a different way "oSomething", but the rest should be similar.

cheers, teylyn
Avatar of tobin46
tobin46

ASKER

In vb.net when I use the syntax above, the columns is looking for a rowindex and column index....

I've tried the syntax above and it didn't work...
Something like

Sheets("Sheet1").Columns(4).ColumnWidth = 10.4

Avatar of tobin46

ASKER

Well....I've set oSheet = oWorkBook.ActiveSheet....wouldn't that be same thing?
ASKER CERTIFIED SOLUTION
Avatar of Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tobin46

ASKER

When I select the property "columns" and use an integer I do not have the option to select "columnwidth".

As I mentioned before, the columns property is looking for a rowindex and columnindex.  MSDN is telling me that the columns property is a Range object that represents all the columns on the worksheet.
Avatar of tobin46

ASKER

Hmmm....just tried the code:

oSheet.columns(integer).columnwidth =2.58 and it worked.....

Thanks!