Microsoft Excel是全球最流行的电子表格之一,用于个人和商业目的。它是以有组织的方式存储、组织和操作数据的一站式目的地。MS Excel主要有两个扩展,即 XLS 和XLSX格式。然而,除了令人难以置信的受欢迎程度之外,运行时错误是许多Windows用户的常见问题——其中最常见的错误之一是运行时错误 1004(Runtime Error 1004)。
在本指南中,我们将讨论这个常见的运行时错误(Runtime error)1004 以及一些可以轻松解决它的最佳修复程序。
Excel中的(Excel)运行时错误 1004(Runtime Error 1004)是什么?
运行时错误 1004 是与(Runtime)Microsoft Visual Basic相关的错误代码,已知会干扰Microsoft Excel用户。任何版本的MS Excel(MS Excel)(例如Excel 2007、2010、2013、2016、2019(Excel 2007))都会遇到此错误。没有任何版本的Microsoft Excel可以免受运行时错误 1004(Runtime Error 1004)的威胁。
此错误主要是用户在处理 excel 文件或尝试在 excel 文档中生成宏(Macro)时遇到的。在使用Visual Basic(Visual Basic) 应用程序(Applications)时,它可能会造成严重的问题,并可能使程序甚至整个系统完全崩溃;有时它可能会冻结系统,禁止用户在其系统上执行任何操作。
错误消息的类型
与此运行时错误最相关的错误消息如下:
- VB:运行时错误“1004”:应用程序定义或对象定义错误
- Excel VBA 运行时(Excel VBA Runtime)错误 1004“范围类的(Range)选择(Select)方法失败”
- 运行时错误1004对象_global(_global)的方法范围失败Visual Basic
- Excel 宏“运行时错误 '1004?
- 运行时(Runtime)错误 1004 方法打开对象工作簿失败
- 运行时错误“1004”:对象“工作表”的方法“游侠”失败
- “ Key up Object Program APPLICATION(Object Program APPLICATION Failed)中的方法(Method)失败。”
如果您遇到上述任何错误,则可以使用我们的指南修复错误。
原因是什么?
错误 1004 是与MS Excel(MS Excel)相关的通用代码,但并不特定于一个确切的原因。因此(Hence),在这种情况下,可能会弹出此错误的确切原因将因情况和情况而异。从配置问题到软件问题,下面我们列出了excel运行时错误1004的常见原因:
- MS Excel 桌面图标(MS Excel Desktop Icon)可能已损坏
- VBA Excel 文件(VBA Excel File)与其他应用程序发生冲突
- 由于应用程序或对象指定错误
- 由于缺少依赖文件
- 由于病毒、木马或恶意软件
- 由于无效的注册表项(Invalid Registry Keys)等。
这些是在MS Excel(MS Excel)中出现运行时错误 1004 的一些最常见原因;现在让我们了解不同的修复。
修复 Excel 中的运行时错误 1004
在这里,我们详细介绍了修复运行时错误 1004(Runtime Error 1004)的手动和自动解决方案。您可以按照以下任何一种方法来解决问题。
- 创建新的 Excel 模板
- 运行病毒扫描
- 对于 VB:运行时错误“1004”,调整图例条目的大小
让我们详细看看这些方法中的每一个。
1]创建一个新的Excel模板
在某些情况下,解决此问题可以像从模板中插入新工作表一样简单,而不是创建现有工作表的副本。这是您需要做的:
1] 在您的系统上打开 MS Excel
2] 按“ CTRL + N ”创建一个新的Microsoft Excel工作表,或者直接从第一个屏幕中选择一个“空白工作簿”。(Blank workbook)
3]完成(Once)后删除工作簿上的所有工作表,除了一张。
4] 现在,格式化留下的工作簿。另外,请注意,可以修改此工作簿以满足您的个人需求。
5] 最后,转到“File > Save As”以Excel 模板(Excel Template)(.xltx 或 .xlt)文件格式保存新工作表。
6]成功创建模板后,您可以使用以下代码行以编程方式插入它:
Sheets.Add Type:=path\filename
请注意 -(Please note – )不要忘记将新文件名替换为文档的实际名称。
2]运行病毒扫描
扫描您的计算机系统中的恶意软件和病毒非常重要,因为它们会损坏文件和重要文档,并在MS Excel中显示运行时错误 1004 。有时使用一个好的防病毒程序会有很大帮助。
相关(Related):运行时错误 1004,无法在 Excel 中运行宏(Run-time error 1004, Cannot run the Macro in Excel)。
3] 对于 VB:运行时错误“1004”,调整图例条目的大小
如果您在运行Microsoft Visual Basic for Applications ( VBA ) 宏时遇到(VBA)运行时(Runtime)错误 1004 ,则可以使用此方法来解决。
通常,当您尝试运行使用LegendEntries方法更改Microsoft Excel图表中的图例条目的VBA宏时,您会收到此错误。那时,您可能会收到以下错误消息:
运行时错误“1004”:应用程序或对象定义的错误
当Excel图表包含的图例条目多于可用于在Excel图表上显示图例条目的空间时,会发生此错误。发生这种情况时,Microsoft Excel可能会截断图例条目。
要解决此问题,请创建一个宏,在VBA宏更改图表图例之前减小(VBA)Excel图表图例文本的字体大小,然后恢复图表图例的字体大小,使其类似于以下宏示例.
Sub ResizeLegendEntries()
With Worksheets("Sheet1").ChartObjects(1).Activate
' Store the current font size
fntSZ = ActiveChart.Legend.Font.Size
'Temporarily change the font size.
ActiveChart.Legend.Font.Size = 2
'Place your LegendEntries macro code here to make
'the changes that you want to the chart legend.
' Restore the font size.
ActiveChart.Legend.Font.Size = fntSZ
End With
End Sub
我们希望本文能帮助您修复Microsoft Excel中的运行时错误 1004 。本指南为您提供手动和自动解决方案以消除此错误;您可以根据需要使用任何解决方案。
阅读下一篇(Read next):箭头键在 Microsoft Excel 中不起作用。
How do I fix Runtime Error 1004 in Microsoft Excel?
Microsoft Excel is one of the most popular spreadsheets used across the globe for both individual and business purposes. It is the one-stop destination for storing, organizing, and manipulating data in an organized way. MS Excel comes mainly in two extensions i.e. XLS and XLSX format. However, apart from its incredible popularity, runtime errors are a common nuisance for a great many Windows users – and one of the most common ones is the Runtime Error 1004.
In this guide, we are going to discuss this common Runtime error 1004 and some of the best fixes to resolve it effortlessly.
What is Runtime Error 1004 in Excel?
Runtime error 1004 is an error code relating to Microsoft Visual Basic that has been known to disturb Microsoft Excel users. This error is faced by any versions of MS Excel such as Excel 2007, 2010, 2013, 2016, 2019 as well. No version of Microsoft Excel is safe from the menace of Runtime Error 1004.
This error is mainly encountered by users while they are working on an excel file or trying to generate a Macro in the excel document. It can cause serious trouble while working with Visual Basic Applications and can completely crash a program, or even the entire system; sometimes it may freeze the system prohibiting the users to do anything on their system.
Types of error message
The error messages that are most associated with this runtime error are as follows:
- VB: run-time error ‘1004’: Application-defined or object-defined error
- Excel VBA Runtime error 1004 “Select method of Range class failed”
- runtime error 1004 method range of object _global failed visual basic
- Excel macro “Run-time error ‘1004?
- Runtime error 1004 method open of object workbooks failed
- Run-Time error ‘1004’: Method ‘Ranger’ of Object’ Worksheet’ Failed
- “Method in Key up Object Program APPLICATION Failed.”
If you encounter any of these above errors, then you can fix the error using our guide.
What are the causes?
The Error 1004 is a general code related to MS Excel but is not specific to one exact cause. Hence, in this case, the exact reason why this error might pop up will vary from case to case and circumstance to circumstance. From configuration issues to software problems, below we listed a synopsis of common reasons for runtime error 1004 in excel:
- MS Excel Desktop Icon might be corrupted
- VBA Excel File is clashing with other application
- Due to application or object specified error
- Due to missing dependent file
- Due to Virus, Trojan or malware
- Due to Invalid Registry Keys and so on.
These were a few of the most common reasons behind getting the runtime error 1004 in MS Excel; now let us understand the different fixes.
Fix Runtime Error 1004 in Excel
Here we have detailed both manual and automatic solutions to fix the Runtime Error 1004. You can follow any one of the next methods to resolve the issue.
- Create a new Excel template
- Run a Virus Scan
- For VB: run-time error ‘1004’, resize legend entries
Let’s look at each of these methods in detail.
1] Create a new Excel template
In some cases, fixing this issue can be as simple as inserting a new worksheet from a template instead of creating a copy of an existing worksheet. Here is what you need to do:
1] Open MS Excel on your system
2] Press ‘CTRL + N’ to create a new Microsoft Excel worksheet or simply select a ‘Blank workbook’ from the first screen.
3] Once done delete all the sheets on the workbook except one.
4] Now, format the workbook, which has been left. Also, note, this workbook can be modified to suit your individual need.
5] In the end, go to ‘File > Save As’ to save the new worksheet with the Excel Template (.xltx or .xlt) file format.
6] Once you have successfully created the template, you can insert it programmatically by using the following line of code:
Sheets.Add Type:=path\filename
Please note – Do not forget to replace the new filename with the actual name of the document.
2] Run a Virus Scan
It is very important to scan your computer system for malware and viruses as these can corrupt files and important documents and show the runtime error 1004 in MS Excel. Sometimes using a good antivirus program helps a lot.
Related: Run-time error 1004, Cannot run the Macro in Excel.
3] For VB: run-time error ‘1004’, resize legend entries
If you come across a Runtime Error 1004 when running a Microsoft Visual Basic for Applications (VBA) macro, then you can use this method to work-around.
Generally, you get this error when you try to run a VBA macro that uses the LegendEntries method to make changes to legend entries in a Microsoft Excel chart. That time, you may receive the following error message:
Run-time error ‘1004’: Application or object-defined error
This error occurs when the Excel chart contains more legend entries than there is space available to display the legend entries on the Excel chart. When this behavior occurs, Microsoft Excel may truncate the legend entries.
To work around this behavior, create a macro that reduces the font size of the Excel chart legend text before your VBA macro makes changes to the chart legend and then restore the font size of the chart legend so that it is similar to the following macro example.
Sub ResizeLegendEntries()
With Worksheets("Sheet1").ChartObjects(1).Activate
' Store the current font size
fntSZ = ActiveChart.Legend.Font.Size
'Temporarily change the font size.
ActiveChart.Legend.Font.Size = 2
'Place your LegendEntries macro code here to make
'the changes that you want to the chart legend.
' Restore the font size.
ActiveChart.Legend.Font.Size = fntSZ
End With
End Sub
We hope this article helps you fix the runtime error 1004 in Microsoft Excel. This guide gives you manual as well as automatic solution to get rid of this error; you can make use of any solution based on your need.
Read next: Arrows keys not working in Microsoft Excel.