【问题标题】:Excel 1004 error on range.valuerange.value 上的 Excel 1004 错误
【发布时间】:2013-02-02 02:24:35
【问题描述】:

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

【问题讨论】:

  • 用作 UDF 的 VBA 函数无法修改工作簿 - 它只能返回一个值或值数组。
  • 正如@TimWilliams 提到的,您不能修改UDF 中除自身之外的其他单元格。你可以在 VBA 中运行函数吗?如果是---> 那就是问题,如果不是--> 最好指定 cur_time 的类型。

标签: excel vba


【解决方案1】:

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

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

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

cur_index = cur_time + 1200 * num_it

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

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

【讨论】:

  • 其实已经定义好了,我这里粘贴的只是我代码的一部分
  • @user511792 啊。我知道了。你能在你的代码中包含更多细节吗?发送到函数的值,例如,其他变量的值等,以便我可以复制错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多