Solved

Formatting Column Widths in Excel Workbook after exporting it from Access

Posted on 2011-03-08
6
567 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

738 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