• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1947
  • Last Modified:

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!  
0
tobin46
Asked:
tobin46
  • 4
  • 3
1 Solution
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
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
0
 
tobin46Author Commented:
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...
0
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Something like

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

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
tobin46Author Commented:
Well....I've set oSheet = oWorkBook.ActiveSheet....wouldn't that be same thing?
0
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Probably. There's a gazillion different ways to specify the object you want to manipulate.

oSheet = oWorkBook.ActiveSheet
oSheet.Columns(4).ColumnWidth = 10.4

Would that work?
0
 
tobin46Author Commented:
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.
0
 
tobin46Author Commented:
Hmmm....just tried the code:

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

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now