Automating open Excel file/Run Script/Then Save Process with a VBA Script

后端 未结 1 465
再見小時候
再見小時候 2021-01-07 14:26

I\'m trying to build a database in Access by importing and appending hundreds of Excel documents in a certain folder together. Each imported excel spreadsheet needs to be ba

1条回答
  •  醉梦人生
    2021-01-07 15:16

    I suggest you use names that will work in Access, that is, no odd characters such as #, and no spaces - it will make your life easier.

    It looks quite unsafe to me to simply change a column heading.

    Const DirOpen As String = "C:\Users\DTurcotte\Desktop\Test_Origin\"
    Const DirSave As String = "C:\Users\DTurcotte\Desktop\Processed\"
    
    Sub Formatting2()
    ''Reference: Windows Script Host Object Model
    ''You could just use late binding, but
    ''the file system object is very useful for this type
    ''of work.
    Dim fs As New FileSystemObject
    Dim fldr As Folder
    Dim f As File
    
    '**'Loop through each xl file in a folder**
    
    If fs.FolderExists(DirOpen) Then
    
        Set fldr = fs.GetFolder(DirOpen)
    
        For Each f In fldr.Files
            If f.Type Like "*Excel*" Then
                ''Includes:
                ''Microsoft Excel 97-2003 Worksheet
                ''Microsoft Excel Comma Separated Values File
                ''Microsoft Excel Macro-Enabled Worksheet
                ''Microsoft Excel Worksheet
                ''Etc
                ProcessFile f.Name
            End If
        Next
    End If
    
    End Sub
    
    
    Sub ProcessFile(FileName As String)
    Dim RowIndex As Integer
    Dim ColIndex As Integer
    ''It is not a good idea to use the names of built-in
    ''objects as variable names
    Dim r As range
    Dim totalRows As Integer
    Dim totalCols As Integer
    Dim LastRow As Long
    
    Dim wb As Workbook
    
    Set wb = Workbooks.Open(DirOpen & FileName)
    
    '**'Format Column Headings
    
    wb.Sheets(1).Select
    
    ''processing code goes here
    
    '**'Saves file to a new folder
    
    wb.SaveAs DirSave & FileName
    wb.Close
    
    End Sub
    

    0 讨论(0)
提交回复
热议问题