我有一个文件夹,其中包含每个员工(工作簿中只有一个工作表)的每个Excel工作簿。我想打开每个员工工作簿并以$ A $ 1读取单元格,然后写入另一个工作表。最终,我将有一个工作表,其中将包含工作簿中的所有$ A $ 1单元。我写了下面的代码,但是我指出的那一行没有出于某种原因。你能建议如何做到这一点?谢谢如何从Excel文件中读取单元格值
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
+1,你说得对。工作簿没有.Range。 –
感谢您的建议!向工作表添加引用的建议完美运行。单元格(i,2)= currentWkbk.worksheets(1).Range(“A1”)。Value。声明一个destWorksheet进行复制时,我仍然遇到一些错误。我会在这方面努力。非常感谢您的帮助! –