运行时错误 1004 Excel 2013 [英] Run-Time error 1004 Excel 2013

查看:32
本文介绍了运行时错误 1004 Excel 2013的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Private Sub CommandButton1_Click()将 lRow 调暗至长Dim ws As 工作表Set ws = Worksheets("Sheet2")lRow = ws.Cells(Rows.Count, 1).End(x1Up).Offset(1, 0).Row与 ws.Cells(lRow, 1).Value = UserForm1.TextBox1.Value.Cells(lRow, 2).Value = UserForm1.TextBox2.Value结束于结束子

我正在使用此宏将一些条目添加到单元格中.我只想将 2 个值添加到文本框中,然后按下按钮将条目移动到 Sheet2 中.第二次,2 个条目将移动到第一个条目下方,依此类推...

每次我按下按钮时,我都会收到消息运行时错误 1004".

解决方案

您的问题已由
简单的变量/常量拼写错误编译器错误(X1UP 应该是 XLUP)

当您在运行时收到错误消息时,转到 VBE 中的子项并点击 F8.编译器将立即发出编译错误消息并突出显示语句的违规部分.

虽然这不能解决所有的编译错误,但它可以让简单的错误快速、容易地识别和纠正.

附录 - 编译错误:语法错误

有一种特殊情况需要处理拼写错误的变量.名称以数字开头的变量的声明、赋值或使用是非法语法.语法错误取代变量未定义错误;有两个错误,但主要报告的错误是语法错误.在这种情况下,整个代码行都会突出显示.


以数字为第一个字符的变量拼写错误编译器错误(1ROW 应为 LROW)

您正在使用显式父工作表引用编写良好的代码,而不依赖于隐式 ActiveSheet 属性.使用需要变量声明选项更进一步.使用 Option Explicit 是被广泛认为是最佳实践".

 Private Sub CommandButton1_Click()
 Dim lRow As Long
 Dim ws As Worksheet
 Set ws = Worksheets("Sheet2")
 lRow = ws.Cells(Rows.Count, 1).End(x1Up).Offset(1, 0).Row
 With ws
.Cells(lRow, 1).Value = UserForm1.TextBox1.Value
.Cells(lRow, 2).Value = UserForm1.TextBox2.Value
 End With
 End Sub

I am using this macro to add some entries into cells. I just want to add the 2 values into textboxes and the pressing the button to move the entries into Sheet2. Second time, the 2 entries will be moved below the first entry and so on...

Every time I am pressing the button I am getting the message "Run-Time error 1004".

解决方案

Your problem has been expertly identified by mongoose36 but here is how to locate and address issues like this in the future.

Compile error: Variable not defined

Setting Require Variable Declaration within the VBE's Tools ► Options ► Editor property page will put the Option Explicit statement at the top of each newly created code sheet. This will avoid silly coding mistakes like misspellings as well as influencing you to use the correct variable type in the variable declaration. Variables created on-the-fly without declaration are all of the variant/object type.

  
         Simple variable/constant misspelling compiler error (X1UP should be XLUP)

When you receive an error during run-time, go to the sub in the VBE and tap F8. The compiler will immediately issue a Compile error message and highlight the offending portion of the statement.

While this will not resolve all compile errors, it makes the simple mistakes quick and easy to identify and correct.

Addendum - Compile error: Syntax error

There is a special case involving misspelled variables that should be addressed. Declaration, assignment or use of a variable with a name that starts with a digit is illegal syntax. The Syntax error supersedes the Variable not defined error; there are two errors but the primary reported error is Syntax error. In this case the entire codeline is highlighted.

  
         Variable misspelling compiler error with digit as first character(1ROW should be LROW)

You are writing good code with explicit parent worksheet references and not relying upon the implicit ActiveSheet property. Go one step further with the Require variable declaration option. Using Option Explicit is widely considered 'best practice'.

这篇关于运行时错误 1004 Excel 2013的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆