我正在 Access 2010 中创建一个表单,它将为特定文件夹中的每个 Excel 文件创建一条记录,并使用来自各个单元格的信息填充字段。该宏使用 Excel 工作簿的所有文件名(各种字母数字序列)填充一个数组,然后遍历这些文件并为每个 Excel 工作表创建一个新记录。
Private Sub PopulateArray_Click()
Dim strListOfFiles() As String
Dim intCount As Integer
intCount = 0
Dim sFile As String
sFile = Dir$("P:\Share\Manufacturing\Propeller\Finalized" & "\*.*", vbDirectory Or vbHidden Or vbSystem Or vbReadOnly Or vbArchive)
Do Until sFile = vbNullString
If sFile <> "." Then ' relative path meaning this directory
If sFile <> ".." Then ' relative path meaning parent directory
ReDim Preserve strListOfFiles(0 To (intCount + 1)) As String
strListOfFiles(intCount) = sFile
intCount = intCount + 1
End If
End If
sFile = Dir$()
Loop
Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset("Record", dbOpenDynaset)
For Index = 0 To UBound(strListOfFiles)
MyRS.AddNew
MyRS![SerialNumber] = "'P:\Share\Manufacturing\Propeller\Finalized\[" & strListOfFiles(Index) & "]Order Input'!B15"
MyRS.Update
Next
End Sub
我已经完成了大部分工作,但最后一步让我卡住了。问题出现在 For 循环中的“MyRS![SerialNumber]”位上。目前它所做的只是打印单元格的(正确)文件路径,而不是单元格本身的值。