[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1634
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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