我有一个按预期工作的VBA程序,然后我将其评论为另一个程序,现在当我再次尝试第一个程序时,它不起作用。我尝试使用代码中计算的值更新一行中的单元格。代码如下,剥离了任何不直接涉及此问题的内容。第一个过程检查值的变化,如果更改,它会在主过程“过程使用”之前调用验证过程。程序......那就是问题所在。
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
有趣的是第一次写'' line正确执行,它是第二个失败的。如果我评论第二个,第三个失败。但是,如果我注释掉第一行,第二行就会起作用而第三行失败,所以似乎没有正确地重置某些东西。我已经看过很多1004个错误帖子,但是找不到一个似乎与我的问题有关的帖子。