我很难过(不需要太多)。我有两段代码似乎不能很好地协同工作。我希望他们或找到替代方案。第一个是清除内容和更改格式的命令按钮代码。在我添加“如果值更改”代码之前,这工作得非常好。值更改代码是指格式从第一个代码更改的范围内的单元格。我想这就是问题所在。代码大部分使用命名范围。我尝试使用单元格地址,在代码中定义范围,保护/取消保护,并将工作表名称添加到范围中。
此代码在没有“如果值更改”代码的情况下工作得很好。
Private Sub CommandButton2_Click()
Sheets("PN Generation").unprotect Password:="1234"
Range("SELECT").Value = 2
Range("PNSELECT").ClearContents
Range("SMC").ClearContents
Range("ALL").Interior.ColorIndex = 35
Range("ALL").Font.ColorIndex = 49
Range("SMC").Interior.ColorIndex = 6
Range("SMCD").Font.ColorIndex = 15
Range("SMCD").Interior.ColorIndex = 15
Call protect
End Sub
使用下面的代码时,上面的代码从第 5 行开始返回 1004 应用程序定义/对象定义错误 - “Range(“ALL”).Interior.ColorIndex=35。”
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D3")) Is Nothing Then
If Range("PNI") = "G" Or Range("PNI") = "U" Or Range("PNI") = "L" Or Range("PNI") = "T" Then
Sheets("Reverse Build").Range("MV").Copy
Sheets("PN Generation").Range("PFLMV").PasteSpecial xlPasteValues
End If
If Range("PNI") = "A" Or Range("PNI") = "B" Or Range("PNI") = "C" Or Range("PNI") = "V" Or Range("PNI") = "AR" Then
Range("LV").Copy
Range("PF").PasteSpecial xlPasteValues
End If
If Range("PNI") = "S" Or Range("PNI") = "P" Then
Range("SIN").Copy
Range("PF").PasteSpecial xlPasteValues
End If
If Range("PNI") = "F" Then
Range("BARE").Copy
Range("PF").PasteSpecial xlPasteValues
End If
If Range("PNI") = "R" Then
Range("LVU").Copy
Range("PF").PasteSpecial xlPasteValues
End If
End Sub
我想了解为什么我会收到错误以及如何修复以防止错误。