我的目标是下拉,以便员工在输入详细信息时不会出错。这是通过使用excel的数据验证功能。
当我在VBE( F5 键)中运行此脚本时,脚本可以正常工作。
但是当我将它放在工作表的Deactivate
事件中时,它会在某一时刻出错。
此行发生错误:
Worksheets("Input").Range("C" & i7).Select
在此代码块中:
Dim Total_rows_Dash7 As Long
Dim Total_rows_Active7 As Long
Dim Total_rows_Input7 As Long
Dim i7 As Long
Dim j7 As Long
Dim m7 As Long
Total_rows_Dash7 = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Dashboard").Range("B" & Rows.count).End(xlUp).Row
For i7 = 2 To Total_rows_Dash7
m7 = 0
Total_rows_Active7 = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Active Jobs").Range("A" & Rows.count).End(xlUp).Row
For j7 = 2 To Total_rows_Active7
If Worksheets("Dashboard").Cells(i7, 2) = Worksheets("Active Jobs").Cells(j7, 1) Then
m7 = 1
End If
Next j7
If m7 = 0 Then
Worksheets("Active Jobs").Cells(Total_rows_Active7 + 1, 1) = Worksheets("Dashboard").Cells(i7, 2)
End If
Next i7
Total_rows_Active7 = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Active Jobs").Range("A" & Rows.count).End(xlUp).Row
Total_rows_Input7 = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Input").Range("A" & Rows.count).End(xlUp).Row
For i7 = 2 To Total_rows_Input7
Worksheets("Input").Range("C" & i7).Select '<-- error occurs here
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="='Active Jobs'!$A$2:$A$" & Total_rows_Active7 & ""
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Next i7
错误如下所示:
运行时错误'1004':选择Range类失败的方法
答案 0 :(得分:2)
根据我在评论中的建议,你可以尝试一下......
For i7 = 2 To Total_rows_Input7
With Worksheets("Input").Range("C" & i7).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="='Active Jobs'!$A$2:$A$" & Total_rows_Active7 & ""
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Next i7