填充单元格时出现错误1004

我有一个VBA程序,按照预期工作,然后我评论它在另一个程序上工作,现在当我再次尝试第一个程序时,它不起作用。 我试图用代码中计算的值更新一行中的单元格。 代码如下,剥离了不直接涉及这个问题的任何东西。 第一个过程检查一个值的变化,如果改变了,它会在主要的'Process_Utilization'过程之前调用一个validation过程…这就是问题所在。

Private Sub Worksheet_Change(ByVal Target As Range) ActiveSheet.Unprotect If Target.Address = Range("start_dt").Address Then 'Verify_start_date Process_Utilization End If If Target.Address = Range("end_dt").Address Then 'Verify_end_date Process_Utilization End If ActiveSheet.Protect End Sub Private Sub Process_Utilization() ' Declare variables Dim r As Integer 'row counter 'The x_col variables below are used to store the column number of the referenced column Dim tot_hrs_col As Integer Dim cost_col As Integer Dim tot_cost_col As Integer Dim total_util_col As Integer Dim avail_hrs As Double 'available hours, based on the date range Dim total_hrs As Double 'total hours ActiveSheet.Unprotect 'Set the column number variables equal to the number of their corresponding named cell tot_hrs_col = Range("est_total_hours").Column cost_col = Range("cost_hr").Column tot_cost_col = Range("total_cost").Column total_util_col = Range("total_util").Column '****************************************************************************** ' Bunch of code to calculate values for the variables '****************************************************************************** ' This line is executed and the cell populated properly Cells(r, total_util_col).Value = total_hrs / avail_hrs ' ****** ERROR ON THE FOLLOWING LINE ******** Cells(r, tot_hrs_col).Value = total_hrs Cells(r, tot_cost_col).Value = total_hrs * Cells(r, cost_col).Value '****************************************************************************** ' More code '****************************************************************************** End Sub 

有趣的是,第一个“写入”行执行正确,第二个失败。 如果我注释掉第二个,第三个就失败了。 但是如果我把第一行注释掉,第二行就行了,第三行失败了,所以看起来有些东西没有被正确地重置。 我已经看到了很多1004错误的线程,但还没有find一个似乎对我的问题说话。