Solved

saving txt as xls with autofit in VBA

Posted on 2001-07-23
6
670 Views
Last Modified: 2007-12-19
The below is an attempt to save a tab-delimited text file into an Excel spreadsheet, with all columns auto-fitted.  The AutoFit doesn't work, and I keep getting prompted to save changes, which won't cut it in a batch environment.  Any suggestions?

Dim Excel As New Excel.Application  
Excel.Workbooks.Open "c:\test.txt"
Excel.Cells.Select
Excel.Selection.Columns.AutoFit
Excel.SaveWorkspace "c:\test.xls"
Excel.Workbooks.Close
Excel.Quit
0
Comment
Question by:arichee
  • 3
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
xSinbad earned 100 total points
ID: 6310465
Try this;

Dim Excel As New Excel.Application  
Excel.Workbooks.Open "c:\test.txt"

Excel.Range("A1").Select

Excel.Selection.Columns.AutoFit
Excel.SaveWorkspace "c:\test.xls"

Excel.Workbooks.Close SaveChanges:=True
Excel.Quit


Cheers
Marcus
0
 

Author Comment

by:arichee
ID: 6310527
I'm getting a VB compile error on Excel.Workbooks.Close SaveChanges:=True.
0
 
LVL 6

Expert Comment

by:xSinbad
ID: 6310544
Sorry;

Workbooks("test.xls").Close SaveChanges:=True
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:nfernand
ID: 6312709
To stop Excel from asking yes/no do:
application.displayalerts = false
0
 

Author Comment

by:arichee
ID: 6313418
Now I'm getting a "Run-time error '9': subscript out of range."

Dim Excel As New Excel.Application
Excel.Application.DisplayAlerts = False
Excel.Workbooks.Open "c:\test.txt"
Excel.Range("A1").Select
Excel.Selection.Columns.AutoFit
Excel.Workbooks("c:\test.xls").Close SaveChanges:=True
Excel.Quit
0
 

Author Comment

by:arichee
ID: 6314782
Your answer was the closest to the solution below.  Thanks.

Dim Excel As New Excel.Application
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
 
Excel.Workbooks.OpenText "c:\file.txt"
 
Set oSheet = Excel.ActiveSheet
Set oRng = oSheet.Range("A1", "Z1")
oRng.EntireColumn.AutoFit
Excel.ActiveWorkbook.RefreshAll
 
Excel.ActiveWorkbook.SaveAs "c:\test.xls", FileFormat:=xlNormal
Excel.Quit
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 2010 repair wrong version 5 55
Excel 2016 - Dotted line appears, but it's not a page break 14 55
MS Office subscription 11 47
Microsoft Edge, Outlook OWA 7 40
Introduction Perhaps more familiar to developers who primarily use VBScript than to developers who tend to work only with Microsoft Office and Visual Basic for Applications (VBA), the Dictionary is a powerful and versatile class, and is useful …
Problem: You created a new custom form in Outlook for your contacts (added fields, deleted fields, changed the layout of fields, whatever) and made it the default form for contacts. The good news is that all new contacts will utilize the new form. T…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

919 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

20 Experts available now in Live!

Get 1:1 Help Now