我有一个文件夹,其中包含每个员工的一个 Excel 工作簿(工作簿中只有一个工作表)。我想打开每个员工工作簿并读取 1 美元的单元格,然后写入另一个工作表。最终,我将有一个工作表,其中包含每个工作簿的所有 $A$1 单元格。我编写了以下代码,但我指出的行由于某种原因不起作用。你能建议怎么做吗?谢谢
Private Sub CommandButton1_Click()
Const FOLDER As String = "c:\Junk\Employee Files\"
On Error GoTo ErrorHandler
Dim i As Integer
i = 0
Dim fileName As String
fileName = Dir(FOLDER, vbDirectory)
Do While Len(fileName) > 0
If Right$(fileName, 4) = "xlsx" Then
i = i + 1
Dim currentWkbk As Excel.Workbook
Set currentWkbk = Excel.Workbooks.Open(FOLDER & fileName)
Cells(i, 1) = "Employee " & 1
'The line above works perfectly
Cells(i, 2) = currentWkbk.Range("A1").Value
'The line doesn't work. above works perfectly
End If
fileName = Dir
Loop
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub