2
我不断收到一个错误1004这条线在我的VBA宏编辑器:应用程序定义或对象定义的错误(EXCEL VBA)
If ActiveCell.Name.Name = "DayShift" Or ActiveCell.Name.Name = "AfterShift" Then
有谁知道为什么吗?这是我的整个宏:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim col As Integer
col = ActiveCell.Column
Dim bValue As String
Dim cValue As String
'Check if cell is required to have both columns with value.
'If it is, skip checks.
If ActiveCell.Name.Name = "DayShift" Or ActiveCell.Name.Name = "AfterShift" Then
End
End If
'Check if active column is column B.
If ColLetter(col) = "B" Then
'Format value of active cell.
cValue = "C" + Str(ActiveCell.Row)
cValue = Replace(cValue, " ", "")
'Check if cell has value.
If Range(cValue) = vbNullString Then
'If it does, remove the opposite shift.
Else
MsgBox "This employee has already been assigned for the afternoon shift. In order to allow this change, this employee's scheduling for the afternoon shift will be removed.", vbExclamation
Range(cValue).ClearContents
End If
'Check if active column is column C.
ElseIf ColLetter(col) = "C" Then
'Format value of active cell.
bValue = "B" + Str(ActiveCell.Row)
bValue = Replace(bValue, " ", "")
'Check if cell has value.
If Range(bValue) = vbNullString Then
'If it does, remove the opposite shift.
Else
MsgBox "This employee has already been assigned for the day shift. In order to allow this change, this employee's scheduling for the day shift will be removed.", vbExclamation
Range(bValue).ClearContents
End If
End If
End Sub
Function ColLetter(ColNumber As Integer) As String
ColLetter = Left(Cells(1, ColNumber).Address(False, False), _
1 - (ColNumber > 26))
End Function
谢谢,这真的有帮助。但是,如果某行在“C”列(下午)中已经有一个值,并且我尝试向“B”列(日)添加值,那么似乎存在问题。它显示应该说明下午班次需要删除的消息,然后显示第二条消息,说明必须删除的日班必须删除B和C列。你知道如何解决这个问题吗? – BioXhazard 2010-07-06 18:11:48
哎呦。需要禁用事件,因为ClearContents调用再次触发该事件。编辑代码来反映。 – 2010-07-06 18:36:28
好的完美。一切都奏效了。谢谢!我想知道你是否可以帮助我处理另一个关于细胞功能的问题。我应该编辑第一篇文章向你展示这个问题吗? – BioXhazard 2010-07-06 18:49:49