Excel 1004 error on range.value

2019-08-20 17:25发布

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

标签: excel vba
1条回答
Ridiculous、
2楼-- · 2019-08-20 18:01

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.

查看更多
登录 后发表回答