VB.Net - Excel Automation Set Column Width on Headings

Jimbo99999
Jimbo99999 used Ask the Experts™
on
Good Day Experts!

Making good progress on my little project and am working on the export to Excel from VB.net.  There really isn't too much data that needs to get pushed across but a formatting issue is troubling me.

I put 8 known column headings in cells 3,1 to 3,8.  Now I need to be able to do the equivalent in code of going to the worksheet and doublicking in between column A and B B and C and so on to get the columns to be as wide as the text that I am putting in 3,1 to 3,8.

How do I do this programatically?

Thanks,
jimbo99999
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:

Author

Commented:
Thanks for responding.  I am having trouble implementing what you have suggested.  

oSheet.Range("A", "H").AutoFit()

I need to get columns A through H as wide as the header text I put in cells 3,1 to 3,8.

I am not sure what the range reference should be.

Thanks,
jimbo99999
NorieAnalyst Assistant
Commented:
There are various ways to do the range reference for this.

Here's one.
oSheet.Columns("A:H").EntireColumn.AutoFit()

Open in new window

Author

Commented:
Works great thanks all.  Not only am I achieving the tasks when I post my questions by using your answers, but accumulating all the links are very helpful as well.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial