Formatting outputted Excel files from Access using VBA?

后端 未结 4 642
面向向阳花
面向向阳花 2021-01-03 17:01

Here I have some VBA code that outputs a ton of files into Excel files. My question is, from this, is there anyway for it to Format the excel file a bit? What I would like t

4条回答
  •  攒了一身酷
    2021-01-03 17:02

    this is a quick and dirty combination of Phil.Wheeler's Code and my previous input, for me this is working. Don't forget to add Excel's Object Library in your Access-Macro.

    Sub doWhatIWantTheDirtyWay()
    
    pathToFolder = "C:\Users\Dirk\Desktop\myOutputFolder\"
    scaleFactor = 0.9
    
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = False
    objExcel.DisplayAlerts = False
    
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFso.GetFolder(pathToFolder)
    
    For Each objFile In objFolder.Files
        If objFso.GetExtensionName(objFile.path) = "xls" Then
             Set objWorkbook = objExcel.Workbooks.Open(objFile.path)
             For Each sh In objWorkbook.Worksheets
    
                If sh.UsedRange.Address <> "$A$1" Or sh.Range("A1") <> "" Then
                    With sh
                        columncount = .Cells(1, 256).End(xlToLeft).Column
                        For j = 1 To columncount
    
                            With .Cells(1, j)
                                i = Len(.Value)
                                .ColumnWidth = i * scaleFactor
                                .Font.Bold = True
                            End With
                        Next
                    End With
                End If
             Next
             objWorkbook.Close True
        End If
    Next
    
    objExcel.Quit
    
    
    
    End Sub
    

提交回复
热议问题