Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 23rd, 2006, 07:02 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default exporting data and reading it in excell

Hi,

I know how to use the transferspreadsheet command in VBA, but what I would like to know is: when I export some data to excell and then I want to read the data I exported in Excell, some cells or not big enough to contain the information, so I always have to resize the cells when I open this excell-file. Is it possible to use a code (or any other way) that could help me to make sure the cell is already that big that the longest data form every column fits the cell so I don't have to resize it every time?

thanks

 
Old May 28th, 2006, 04:09 PM
Authorized User
 
Join Date: Jul 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try opening a recordset, looping thru, and then use Autofit

Dim StrWorksheet as string
Dim StrWorksheetPath as string
Dim appExcel as excel.Application
Dim StrTemplatePath as string
Dim i as integer
Dim bks as Excel.Workbooks
Dim Wks as Excel.Worksheet
Dim rs as recordset

Set appExcel = New Excel.Application
StrTemplatePath = "C:\YrExcelPath"
StrWorksheet = "The_Name_of_Your_Workbook.xls"
StrWorkSheetPath = StrTemplatePath & StrWorksheet
Set bks = appExcel.workbooks

'Open the workbook
bks.open strWorksheetPath
Set wks = appExcel.Sheets("Report")
appExcel.DisplayAlerts = False

With wks

'----Delete prior data-----
wks.Range("A:DD").Delete

'----loop thru and add data to excel----
Set rs = currentdb.openrecordset("MyQry")

For i = 1 to rs.recordcount
wks.Range("A1").offset(i,0).value = rs!Type
wks.Range("B1").offset(i,0).value = rs!Type
rs.movenext
Next i

rs.Close


'----AutoFit your Columns-----
Wks.Range("A:DD").Columns.Autofit

appExcel.Application.Visible = True
appExcel.DisplayAlerts = True
End with




 
Old May 28th, 2006, 04:12 PM
Authorized User
 
Join Date: Jul 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

PS - you have to have Microsoft Excel 11.0 Object Library referenced:

Tools/Refrence/then scroll down and select






Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting data to Excel lrickett Beginning VB 6 3 February 10th, 2011 01:27 AM
ADO Problem fro extracting excell data mmmhbd ADO.NET 1 May 20th, 2006 02:50 AM
Export data to several excell sheets at will milocold Crystal Reports 0 November 16th, 2005 11:42 AM
Query data and exporting data from one sheet to an testman Excel VBA 2 April 28th, 2005 02:40 PM
Import Excell Data into Access Mitch Access 4 February 15th, 2005 10:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.