Solved

Format row height and column width during Excel export

Posted on 2009-04-10
5
627 Views
Last Modified: 2013-11-28
I am exporting to Excel via VBA a large, by row and column, data set. When I do, the columns and rows are both sized poorly. Would like to know how to set this at export. I'd prefer not to use an Excel template since I'd have to distribute it to a number of users.

Thanks in advance.
0
Comment
Question by:dglover
  • 4
5 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 24120783
dglover,

The Export Command cannot format the Excel Column Widths, you will have to do this via code as well.

Put something like this on the end of your Export code:

Dim objXL As Object
Set objXL = CreateObject("Excel.Application")
    objXL.Visible = False
    objXL.Workbooks.Open "C:\YourFolder\YourFile.xls"
    objXL.activesheet.Cells.select
    objXL.activesheet.Cells.EntireColumn.AutoFit
    objXL.activesheet.Range("A1").select
    objXL.activeWorkbook.Save
    objXL.activeWorkbook.Close False
    objXL.Quit
End Sub

JeffCoachman


0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 250 total points
ID: 24120822
Sorry, that code will only autofit the Columns.

Here is the code that will autofit the Rows as well:

Dim objXL As Object
Set objXL = CreateObject("Excel.Application")
    objXL.Visible = False
    objXL.Workbooks.Open "c:\tblDetailVideos.xls"
    objXL.activesheet.Cells.select
    objXL.activesheet.Cells.EntireColumn.AutoFit
    objXL.activesheet.Cells.EntireRow.AutoFit
    objXL.activesheet.Range("A1").select
    objXL.activeWorkbook.Save
    objXL.activeWorkbook.Close False
    objXL.Quit


Use like this:

Sub YourCode()
Dim objXL As Object
Set objXL = CreateObject("Excel.Application")

    'Your Code
    objXL.Visible = False
    objXL.Workbooks.Open "c:\tblDetailVideos.xls"
    objXL.activesheet.Cells.select
    objXL.activesheet.Cells.EntireColumn.AutoFit
    objXL.activesheet.Cells.EntireRow.AutoFit
    objXL.activesheet.Range("A1").select
    objXL.activeWorkbook.Save
    objXL.activeWorkbook.Close False
    objXL.Quit

'...
End Sub

JeffCoachman
   
     
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 24120828
Obviously you will substitute your Excel file for mine
;-)

Jeff
0
 

Author Closing Comment

by:dglover
ID: 31569106
Jeff, Thanks. This took care of it.  
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 24123246
;-D
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question