Solved

Format row height and column width during Excel export

Posted on 2009-04-10
5
612 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now