Excel 1004错误在range.value上 [英] Excel 1004 error on range.value
问题描述
VBE调试器不断给我
1004错误(应用程序定义或对象定义的错误)
/ p>
在线工作表(常规)。单元格(cur_index,2).Value = cur_time
p>
或尝试修改单元格值的任何尝试。
该功能从同一工作表中的单元格调用。但是当我在即时窗口中输入?
活动表单时,没有任何显示。有什么问题吗?
有人可以告诉我如何解决这个问题吗?谢谢。
更新:这是我的代码的一部分,我的代码开头我有选项显式。
函数collectdata(cur_time)
Dim row_num As Integer,start_index As Integer,end_index As Integer,cur_index As Integer,col_num As Integer
row_num = 1
start_index = total_record * num_it + 21
end_index = start_index + total_record - 1
Dim rg As String
rg =A& start_index& :A& end_index
错误GoTo err1
如果cur_time> 0和num_it < 10然后
cur_index = cur_time + 1200 * num_it
工作表(常规)。单元格(cur_index,2).Value = cur_time
Worksheets(General)。Range(rg).Value = num_it + 1
对于col_num = 3到12
工作表(一般)。单元格(cur_index,col_num).Value = Cells(7,col_num).Value
下一个col_num
ElseIf cur_time = 1200然后
num_it = num_it + 1
End If
err1:
Debug.Print Err.Description
结束函数
这可能不会完全回答你的问题,但是我希望帮助你修复你的代码
将 Option Explicit
放在代码的顶部。此选项将帮助您以更强类型的方式编写代码。
另外,您的 num_it
变量是未定义的,因此它是空的。这行:
cur_index = cur_time + 1200 * num_it
转动 cur_index
= 0.然后, Cells(row,column)
方法给出错误,因为没有工作表有一行0.行和列从1开始。
另外,将数据类型设置为参数 cur_time
。例如,如果您传递一个字符串,您的代码将会出错。
The VBE debugger keeps give me
1004 error (Application-defined or object-defined error)
on line "Worksheets("General").Cells(cur_index, 2).Value = cur_time"
or any attempts that I try to modify the cell value really.
The function is called from a cell in the same worksheet. but when I type ?
activesheet in the immediate window, there's nothing showing up. what's wrong?
Can someone tell me how to fix it and what's the problem? Thanks.
UPDATE: this is part of my code and i do have option explicit at the beginning of my code.
Function collectdata(cur_time)
Dim row_num As Integer, start_index As Integer, end_index As Integer, cur_index As Integer, col_num As Integer
row_num = 1
start_index = total_record * num_it + 21
end_index = start_index + total_record - 1
Dim rg As String
rg = "A" & start_index & ":A" & end_index
On Error GoTo err1
If cur_time > 0 And num_it < 10 Then
cur_index = cur_time + 1200 * num_it
Worksheets("General").Cells(cur_index, 2).Value = cur_time
Worksheets("General").Range(rg).Value = num_it + 1
For col_num = 3 To 12
Worksheets("General").Cells(cur_index, col_num).Value = Cells(7, col_num).Value
Next col_num
ElseIf cur_time = 1200 Then
num_it = num_it + 1
End If
err1:
Debug.Print Err.Description
End Function
This may not answer your question exactly, but I wish to help you fix your code a bit.
Put Option Explicit
at the top of your code. This option will help you write your code in a more "strongly typed" manner.
Also, your num_it
variable is undefined, and so it is empty. This line:
cur_index = cur_time + 1200 * num_it
Turns cur_index
= 0. Then, the Cells(row, column)
method gives the error because no worksheet has a row of 0. Rows and columns start at 1.
Also, set a data type to the parameter cur_time
. Your code will error out if you pass a string to it, for example.
这篇关于Excel 1004错误在range.value上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!