?
Solved

Formatting Column Widths in Excel Workbook after exporting it from Access

Posted on 2011-03-08
6
Medium Priority
?
571 Views
Last Modified: 2012-05-11
I have an Access 2003 application that creates an Excel Workbook containing about 12 worksheets.  After the workbook gets created I have to go into it and format the column widths on each worksheet.  Is there a way to do this formatting using vba code?
0
Comment
Question by:dsoderstrom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35074175
here is  a sample code to do formatting
Private Sub FormatSheet(objXLSheet As Object)
With objXLSheet
        .Range("D2", .Range("D2").End(xlToRight).End(xlDown)).NumberFormat = "#,###.00"
        .Range("C2", .Range("C2").End(xlDown)).NumberFormat = "0.00%"
        .Range("A1", .Range("A1").End(xlToRight).End(xlDown)).Columns.AutoFit
        .Range("A1", .Range("A1").End(xlToRight)).Font.Bold = True
        .Range("A1", .Range("A1").End(xlToRight)).HorizontalAlignment = xlCenter
        .Range("A1").CurrentRegion.Select
End With
End Sub

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35074185
post the codes that you are using to create the excel file
0
 

Author Comment

by:dsoderstrom
ID: 35084030
The code I am using is as follows:

Dim iselecfile
    iselecfile = Dir("c:\Raw Material Spreadsheet.xls")
    If iselecfile <> "" Then
        Kill ("c:\Raw Material Spreadsheet.xls")
    End If
    DoCmd.OpenQuery "Clear Selected Comm Code List"
    DoCmd.OpenQuery "Load Selected Comm Code List-B"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Aluminum Extrusion", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Aluminum Sheet", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Hex Shaft", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Oval", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Angle", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Channel", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Pipe", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Round Tube", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Flat Bar", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Strip", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Expanded Metal", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Plate", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Sheet", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Plow Share-Beveled Bar", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Rectangular Tube", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Square Tube", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Round", "c:\Raw Material Spreadsheet.xls", True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Threaded Rod", "c:\Raw Material Spreadsheet.xls", True
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:dsoderstrom
ID: 35097085
Still don't know how to do this based on earlier reply.
I need to know what the code is for opening the Excel workbook and then stepping through the worksheets changing the column widths on each one.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 35097313
to open the excel file using codes and set column widths

dim xlObj as object, j as integer
set xlObj=createobject("excel.application")
     xlobj.workbooks.open("c:\Raw Material Spreadsheet.xls")
     with xlObj
         for j=1 to .worksheets.count
              .worksheets(j).select
              .range("a:b").columnwidth=12              


         next
         .activeworkbook.save
     end with
     xlobj.quit
0
 

Author Closing Comment

by:dsoderstrom
ID: 35123651
This worked perfectly.  Thanks so much for the help.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

762 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