【发布时间】:2010-12-28 13:38:23
【问题描述】:
我正在尝试通过在某个文件夹中导入和附加数百个 Excel 文档来在 Access 中构建一个数据库。每个导入的 Excel 电子表格要正确附加到 Access 中的最后一个 Excel 电子表格,需要基本统一。此外,单元格中的空格会导致访问问题... 由于有数百个 Excel 文件要添加到 Access,我希望使用 VBA 来自动化该过程......所以这就是我想要完成的:
1st) 宏首先扫描包含我希望导入的所有 Excel 电子表格的文件夹...并一次自动打开一个 Excel 文件。 2nd)检查该excel文件以查看所有空格是否都用“ - ”填充 3)如果是,将更新后的 excel 副本保存到我命名为“新项目”的文件夹中 4)在下一个电子表格上重复过程
这是我到目前为止编写的代码.. 但无法拥有它从特定文件夹中自动打开我需要的每个文件,运行脚本的其余部分,然后保存它...
Sub Formatting()
Dim counter As Integer
Dim TotalFiles As Integer
TotalFiles = 1
**'Loop through each xl file in a folder**
For counter = 1 To TotalFiles
**'Open multiple Files----------------------------------------------------------------------------------------------**
Dim Filter As String, Title As String, msg As String
Dim i As Integer, FilterIndex As Integer
Dim xlFile As Variant
Filter = "Excel Files (*.xls), *.xls," & "Text Files (*.txt), *.txt," & "All files (*.*), *.*"
**'Default filter = *.***
FilterIndex = 3
**'Set dialog caption**
Title = "Select File(s) to Open"
**'Select Start and Drive path**
ChDrive ("C")
ChDir ("C:\Users\DTurcotte\Desktop\Test_Origin")
With Application
**'Set file name array to selected files (allow multiple)**
xlFile = .GetOpenFilename(Filter, FilterIndex, Title, , True)
**'Reset Start Drive/Path**
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
**'Exit on Cancel**
If Not IsArray(xlFile) Then
MsgBox "No file was selected."
Exit Sub
End If
**'Open Files**
For i = LBound(xlFile) To UBound(xlFile)
msg = msg & xlFile(i) & vbCrLf
Workbooks.Open xlFile(i)
Next i
MsgBox msg, vbInformation, "Files Opened"
**'Format Column Headings----------------------------------------------------------------------------------------------**
ActiveWorkbook.Sheets.Select
Dim RowIndex As Integer
Dim ColIndex As Integer
Dim totalRows As Integer
Dim totalCols As Integer
Dim LastRow As Long
Dim range As range
totalRows = Application.WorksheetFunction.CountA(Columns(1))
If Cells(1, 1).Value <> "ROOM #" Then Cells(1, 1).Value = "ROOM #"
If Cells(1, 2).Value <> "ROOM NAME" Then Cells(1, 2).Value = "ROOM NAME"
If Cells(1, 3).Value <> "HOMOGENEOUS AREA" Then Cells(1, 3).Value = "HOMOGENEOUS AREA"
If Cells(1, 4).Value <> "SUSPECT MATERIAL DESCRIPTION" Then Cells(1, 4).Value = "SUSPECT MATERIAL DESCRIPTION"
If Cells(1, 5).Value <> "ASBESTOS CONTENT (%)" Then Cells(1, 5).Value = "ASBESTOS CONTENT (%)"
If Cells(1, 6).Value <> "CONDITION" Then Cells(1, 6).Value = "CONDITION"
If Cells(1, 7).Value <> "FLOORING (SF)" Then Cells(1, 7).Value = "FLOORING (SF)"
If Cells(1, 8).Value <> "CEILING (SF)" Then Cells(1, 8).Value = "CEILING (SF)"
If Cells(1, 9).Value <> "WALLS (SF)" Then Cells(1, 9).Value = "WALLS (SF)"
If Cells(1, 10).Value <> "PIPE INSULATION (LF)" Then Cells(1, 10).Value = "PIPE INSULATION (LF)"
If Cells(1, 11).Value <> "PIPE FITTING INSULATION (EA)" Then Cells(1, 11).Value = "PIPE FITTING INSULATION (EA)"
If Cells(1, 12).Value <> "DUCT INSULATION (SF)" Then Cells(1, 12).Value = "DUCT INSULATION (SF)"
If Cells(1, 13).Value <> "EQUIPMENT INSULATION (SF)" Then Cells(1, 13).Value = "EQUIPMENT INSULATION (SF)"
If Cells(1, 14).Value <> "MISC. (SF)" Then Cells(1, 14).Value = "MISC. (SF)"
If Cells(1, 15).Value <> "MISC. (LF)" Then Cells(1, 15).Value = "MISC. (LF)"
**'Fills in blank spaces with "-"**
For RowIndex = 1 To totalRows
For ColIndex = 1 To 15
If Cells(RowIndex, ColIndex).Value = "" Then Cells(RowIndex, ColIndex).Value = "test"
Next ColIndex
Next RowIndex
**'Clears content from "Totals" Row**
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Rows(LastRow).ClearContents
**'Saves file to a new folder
'Need to have the code run through that excel doc, set that updated copy to a variable, and then have the following code save it to a new folder**
***ToDo***
**'newSaveName = updated excel file**
'ActiveWorkbook.SaveAs ("C:\Users\DTurcotte\Desktop\TestExcelFiles" & Test1_Success & ".xls")
Next counter
End Sub
谁能提供帮助?
【问题讨论】:
-
工具栏上的花括号 {} 符号允许您在发帖时格式化代码,这将使您的帖子更具可读性并且更有可能得到答案。