range.value上的Excel 1004错误

时间:2013-02-01 15:50:07

标签: excel vba

VBE调试器一直给我

1004 error (Application-defined or object-defined error)

"Worksheets("General").Cells(cur_index, 2).Value = cur_time"

或者我尝试修改单元格值的任何尝试。

从同一工作表中的单元格调用该函数。但是当我输入?

在即时窗口中的

活动表,没有任何显示。怎么了?

有人可以告诉我如何修复它以及问题是什么?感谢。

更新:这是我的代码的一部分,我在代码的开头有明确的选项。

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

1 个答案:

答案 0 :(得分:0)

这可能无法准确回答您的问题,但我希望帮助您修改一下代码。

Option Explicit放在代码顶部。此选项将帮助您以更“强类型”的方式编写代码。

此外,您的num_it变量未定义,因此它是空的。这一行:

cur_index = cur_time + 1200 * num_it

转动cur_index = 0.然后,Cells(row, column)方法给出错误,因为没有工作表的行为0.行和列从1开始。

另外,将数据类型设置为参数cur_time。例如,如果您将字符串传递给它,您的代码将会出错。

相关问题