Excel - 运行时错误 1004"应用程序定义或对象定义错误"



我有一个带有VBA的Excel 2003电子表格("Tune.xls"(。它总是失败并出现错误:

运行时错误1004"应用程序定义或对象定义错误">

当我单击debug时,它会将我带到代码中的以下行:

DataImport.Range("J10").Value = FileName 'Copy path and filename to worksheet "Import Data"

还请查看整个代码如下:

'Ensure that all global (public) flags are initialized properly; they might have been
'changed before the user requested a data import.
Sub_Error = False
Changed_Model_1 = False
Changed_Model_2 = False
Plot_Model_1 = False
Plot_Model_2 = False
Updated_Model_1 = False
Updated_Model_2 = False
'Disable screen updating to avoid flicker when graph data is modified. Puting the focus away
'from the graph to worksheet "Data Import" speeds up the execution by a factor of about 4. This trick
'is ad hoc and I cannot explain why it works, but it does...
Application.ScreenUpdating = False
DataImport.Select
'Open window to select data file. If no file is opened, then the variable FileName will be false.
FileName = Application.GetOpenFilename("Text Files (*.txt; *.dat),*.txt;*.dat, Excel Files (*.xls),*.xls", , "Open Process Data File")
If FileName = False Then Exit Sub     'exit if no file was opened or cancel button selected
******DataImport.Range("J10").Value = FileName  'Copy path and filename to worksheet "Import Data"******
'Open data file as a workbook and use shortcut name "DataSheet" for worksheet containing data.
Workbooks.Open FileName:=FileName, updateLinks:=0        'does not update linked cells
Set DataSheet = ActiveWorkbook.ActiveSheet
Set DataBook = ActiveWorkbook                            'use shortcut for Data workbook
'The data workbook might contain cells referencing a DDE link. This makes it hard to manipulate
'the cells. Therefore, the entire worksheet is copied and only the values are pasted back. This
'will essentially remove all the DDE references.
DataSheet.Cells.Select                      'This selects all cells
Selection.Copy
Selection.PasteSpecial Paste:=xlValues

有人能帮我把这件事做好吗?

1004是一个错误,它可能由于各种原因而出现。一般来说,这可能是因为:

  • 有些对象定义不正确
  • 工作表被锁定

因此,要检查第一个选项,请在错误之前写入这两行:

MsgBox Filename
MsgBox DataImport.Name

看看你得到了什么。您应该得到一些文件名(不是错误(,DataImport.Name应该是图纸的名称。

关于第二个选项-检查DataImport中的单元格是否未锁定。

最新更新