将数据从一个Excel电子表格导出到另一个电子表格-运行时错误1004

时间:2018-07-27 04:57:25

标签: excel export runtime

我正在尝试使用将数据导出到中央excel电子表格的按钮来设置excel电子表格。 我有以下代码应该具有我需要的所有功能:

Sub Export()
If MsgBox("Are you sure you want to export all data entered?", vbYesNo + vbInformation) = vbNo Then Exit Sub
' Test to see if the file is open by someone else already.
Application.ScreenUpdating = False
If IsFileOpen("C:\Users\rhopcroft\Documents\Ryan's stuff\Test files for import\Tester\Excel import test tile.xlsm", password:="123") Then
MsgBox "Your data hasn't been exported. This is probably because another user was exporting their data when you tried to. Please try again in a moment."

Else


Range("A1:F3").Copy
Workbooks.Open filename:="C:\Users\rhopcroft\Documents\Ryan's stuff\Test files for import\Tester\Excel import test tile.xlsm", password:="123"
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
ActiveWorkbook.Save
ActiveWindow.Close
Range("A1").Select
Application.ScreenUpdating = True
MsgBox ("Thanks, your data has been successfully exported.")
End If


 ' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.
Function IsFileOpen(filename As String, password As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function

所以我得到了 “运行时错误'1004':应用程序定义或对象定义的错误” 到

Workbooks.Open filename:="C:\Users\Documents\Test files for import\Tester\Excel import test tile.xlsm", password:="123"
    Range("A" & Rows.Count).End(xlUp).Offset(1).Select

在这一点上,我只想使它适用于两个测试文件。所需的数据在A1:F3中-尽管实际上,我希望它将按钮上的所有数据都放在工作表上,然后将其添加到映射的工作簿中表格的底部。

任何帮助都将不胜感激。

0 个答案:

没有答案