?
Solved

saving txt as xls with autofit in VBA

Posted on 2001-07-23
6
Medium Priority
?
678 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 300 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

864 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