我遇到“运行时错误”1004:当我包含代码时,对象'Interior'的方法'颜色'失败“错误
Worksheet.Protect UserInterfaceOnly := True
在我的代码中。
我的目标是保护工作表中的某些单元格,同时允许用户修改数据输入所必需的其他单元格。我对VBA编码比较新。任何有经验的程序员可能提供的任何见解都将非常非常感激。
我正在使用的代码粘贴在下面。我收到1004错误的第一行是:
Asheet.Range("a4").Interior.Color = RGB(204, 204, 255)
非常感谢!
我的代码:
Sub BowelHelathMacro()
'Declaring Variables
Dim Asheet As Worksheet
Dim Dsheet As Worksheet
Set Asheet = Sheets("Analysis")
Set Dsheet = Sheets("Data")
'Locking Cells/Spreadsheet(s)
'Unprotect Cells:
Dsheet.Range("c3:e13").locked = False
Dsheet.Range("c3:e13").FormulaHidden = False
Asheet.Range("g5:h15").locked = False
Asheet.Range("g5:h15").FormulaHidden = False
' 'Protect Sheets:
Dsheet.Protect UserInterfaceOnly:=True
Asheet.Protect UserInterfaceOnly:=True
'Clear Previous Day's Spreadsheet
Asheet.Range("a1:o15").Clear
'Insert Headers
'**CONSIDER ADDING "thisworkbook.sheets..." in order to prevent overwriting
Asheet.Range("a4") = "Date"
Asheet.Range("b4") = "Caution?"
Asheet.Range("c4") = "Last"
Asheet.Range("d4") = "First"
Asheet.Range("e4") = "Last Bowel Movement"
Asheet.Range("f4") = "Prune Juice Administered Last Night?"
Asheet.Range("g4") = "Action Steps"
Asheet.Range("h4") = "Comments"
Asheet.Select
Asheet.Range("a4:h4").Select
Selection.Font.Bold = True
'Color Interior of Analysis Headers
Asheet.Range("a4").Interior.Color = RGB(204, 204, 255)
Asheet.Range("b4").Interior.Color = RGB(300, 75, 75)
Asheet.Range("c4").Interior.Color = RGB(204, 204, 255)
Asheet.Range("d4").Interior.Color = RGB(204, 204, 255)
Asheet.Range("e4").Interior.Color = RGB(255, 255, 100)
Asheet.Range("f4").Interior.Color = RGB(255, 255, 100)
Asheet.Range("g4").Interior.Color = RGB(300, 75, 75)
Asheet.Range("h4").Interior.Color = RGB(204, 204, 255)
'Insert Key for Colors: Orange & Blue
Asheet.Range("a1").Interior.Color = RGB(255, 153, 0)
Asheet.Range("a2").Interior.Color = RGB(153, 204, 255)
Asheet.Range("b1") = "Caution: Potential Bowel Health Complications"
Asheet.Range("b2") = "Bowel Movement within Healthy Parameters"
Asheet.Range("a1:a2").Select
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'Insert Updated Date Range (2 weeks back) into Data Tab
Dsheet.Select
Range("i3") = Date - 14
Range("i4") = Date - 13
Range("i5") = Date - 12
Range("i6") = Date - 11
Range("i7") = Date - 10
Range("i8") = Date - 9
Range("i9") = Date - 8
Range("i10") = Date - 7
Range("i11") = Date - 6
Range("i12") = Date - 5
Range("i13") = Date - 4
Range("i14") = Date - 3
Range("i15") = Date - 2
Range("i16") = Date - 1
Range("i17") = Date
'Copy & Paste Current Individual Names from "Data" Tab
Dsheet.Select
Range("A3:B13").Select
Selection.Copy
Asheet.Select
Range("c5").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
'Insert Today's Date
For x = 5 To 15
Cells(x, 1) = Date
Next x
'ANALYSIS: (1) Caution or Non-Caution Analysis; (2) Last Bowel Movement Analysis
'Individual Position 01
If Dsheet.Range("c3") < Dsheet.Range("i15") Then
Asheet.Range("b5").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e5") = "Last bowel movement on " & Dsheet.Range("m3") & ", MORE than two days ago."
End If
If Dsheet.Range("c3") >= Dsheet.Range("i15") Then
Asheet.Range("b5").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e5") = "Last bowel movement on " & Dsheet.Range("m3") & "." & " Within healthy tolerance levels."
End If
'Individual Position 02
If Dsheet.Range("c4") < Dsheet.Range("i15") Then
Asheet.Range("b6").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e6") = "Last bowel movement on " & Dsheet.Range("m4") & ", MORE than two days ago."
End If
If Dsheet.Range("c4") >= Dsheet.Range("i15") Then
Asheet.Range("b6").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e6") = "Last bowel movement on " & Dsheet.Range("m4") & "." & " Within healthy tolerance levels."
End If
'Individual Position 03
If Dsheet.Range("c5") < Dsheet.Range("i15") Then
Asheet.Range("b7").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e7") = "Last bowel movement on " & Dsheet.Range("m5") & ", MORE than two days ago."
End If
If Dsheet.Range("c5") >= Dsheet.Range("i15") Then
Asheet.Range("b7").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e7") = "Last bowel movement on " & Dsheet.Range("m5") & "." & " Within healthy tolerance levels."
End If
'Individual Position 04
If Dsheet.Range("c6") < Dsheet.Range("i15") Then
Asheet.Range("b8").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e8") = "Last bowel movement on " & Dsheet.Range("m6") & ", MORE than two days ago."
End If
If Dsheet.Range("c6") >= Dsheet.Range("i15") Then
Asheet.Range("b8").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e8") = "Last bowel movement on " & Dsheet.Range("m6") & "." & " Within healthy tolerance levels."
End If
'Individual Position 05
If Dsheet.Range("c7") < Dsheet.Range("i15") Then
Asheet.Range("b9").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e9") = "Last bowel movement on " & Dsheet.Range("m7") & ", MORE than two days ago."
End If
If Dsheet.Range("c7") >= Dsheet.Range("i15") Then
Asheet.Range("b9").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e9") = "Last bowel movement on " & Dsheet.Range("m7") & "." & " Within healthy tolerance levels."
End If
'Individual Position 06
If Dsheet.Range("c8") < Dsheet.Range("i15") Then
Asheet.Range("b10").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e10") = "Last bowel movement on " & Dsheet.Range("m8") & ", MORE than two days ago."
End If
If Dsheet.Range("c8") >= Dsheet.Range("i15") Then
Asheet.Range("b10").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e10") = "Last bowel movement on " & Dsheet.Range("m8") & "." & " Within healthy tolerance levels."
End If
'Individual Position 07
If Dsheet.Range("c9") < Dsheet.Range("i15") Then
Asheet.Range("b11").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e11") = "Last bowel movement on " & Dsheet.Range("m9") & ", MORE than two days ago."
End If
If Dsheet.Range("c9") >= Dsheet.Range("i15") Then
Asheet.Range("b11").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e11") = "Last bowel movement on " & Dsheet.Range("m9") & "." & " Within healthy tolerance levels."
End If
'Individual Position 08
If Dsheet.Range("c10") < Dsheet.Range("i15") Then
Asheet.Range("b12").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e12") = "Last bowel movement on " & Dsheet.Range("m10") & ", MORE than two days ago."
End If
If Dsheet.Range("c10") >= Dsheet.Range("i15") Then
Asheet.Range("b12").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e12") = "Last bowel movement on " & Dsheet.Range("m10") & "." & " Within healthy tolerance levels."
End If
'Individual Position 09
If Dsheet.Range("c11") < Dsheet.Range("i15") Then
Asheet.Range("b13").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e13") = "Last bowel movement on " & Dsheet.Range("m11") & ", MORE than two days ago."
End If
If Dsheet.Range("c11") >= Dsheet.Range("i15") Then
Asheet.Range("b13").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e13") = "Last bowel movement on " & Dsheet.Range("m11") & "." & " Within healthy tolerance levels."
End If
'Individual Position 10
If Dsheet.Range("c12") < Dsheet.Range("i15") Then
Asheet.Range("b14").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e14") = "Last bowel movement on " & Dsheet.Range("m12") & ", MORE than two days ago."
End If
If Dsheet.Range("c12") >= Dsheet.Range("i15") Then
Asheet.Range("b14").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e14") = "Last bowel movement on " & Dsheet.Range("m12") & "." & " Within healthy tolerance levels."
End If
'Individual Position 11
If Dsheet.Range("c13") < Dsheet.Range("i15") Then
Asheet.Range("b15").Interior.Color = RGB(255, 153, 0)
Asheet.Range("e15") = "Last bowel movement on " & Dsheet.Range("m13") & ", MORE than two days ago."
End If
If Dsheet.Range("c13") >= Dsheet.Range("i15") Then
Asheet.Range("b15").Interior.Color = RGB(153, 204, 255)
Asheet.Range("e15") = "Last bowel movement on " & Dsheet.Range("m13") & "." & " Within healthy tolerance levels."
End If
'ANALYSIS: Prune Juice Administration
'Individual 01
Asheet.Range("f5") = Dsheet.Range("e3") & "."
'Individual 02
Asheet.Range("f6") = Dsheet.Range("e4") & "."
'Individual 03
Asheet.Range("f7") = Dsheet.Range("e5") & "."
'Individual 04
Asheet.Range("f8") = Dsheet.Range("e6") & "."
'Individual 05
Asheet.Range("f9") = Dsheet.Range("e7") & "."
'Individual 06
Asheet.Range("f10") = Dsheet.Range("e8") & "."
'Individual 07
Asheet.Range("f11") = Dsheet.Range("e9") & "."
'Individual 08
Asheet.Range("f12") = Dsheet.Range("e10") & "."
'Individual 08
Asheet.Range("f13") = Dsheet.Range("e11") & "."
'Individual 09
Asheet.Range("f14") = Dsheet.Range("e12") & "."
'Individual 10
Asheet.Range("f15") = Dsheet.Range("e13") & "."
'Individual 11
Asheet.Range("f16") = Dsheet.Range("e14") & "."
'ANALYSIS: Action Steps
'Individual 01
'If Orange:
If Asheet.Range("b5").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g5") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g5").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b5").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e3") = "Yes" Then
Asheet.Range("g5") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b5").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e3") = "No" Then
Asheet.Range("g5") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 02
'If Orange:
If Asheet.Range("b6").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g6") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g6").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b6").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e4") = "Yes" Then
Asheet.Range("g6") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b6").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e4") = "No" Then
Asheet.Range("g6") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 03
'If Orange:
If Asheet.Range("b7").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g7") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g7").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b7").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e5") = "Yes" Then
Asheet.Range("g7") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b7").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e5") = "No" Then
Asheet.Range("g7") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 04
'If Orange:
If Asheet.Range("b8").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g8") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g8").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b8").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e6") = "Yes" Then
Asheet.Range("g8") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b8").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e6") = "No" Then
Asheet.Range("g8") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 05
'If Orange:
If Asheet.Range("b9").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g9") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g9").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b9").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e7") = "Yes" Then
Asheet.Range("g9") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b9").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e7") = "No" Then
Asheet.Range("g9") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 06
'If Orange:
If Asheet.Range("b10").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g10") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g10").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b10").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e8") = "Yes" Then
Asheet.Range("g10") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b10").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e8") = "No" Then
Asheet.Range("g10") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 07
'If Orange:
If Asheet.Range("b11").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g11") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g11").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b11").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e9") = "Yes" Then
Asheet.Range("g11") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b11").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e9") = "No" Then
Asheet.Range("g11") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 08
'If Orange:
If Asheet.Range("b12").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g12") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g12").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b12").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e10") = "Yes" Then
Asheet.Range("g12") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b12").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e10") = "No" Then
Asheet.Range("g12") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 09
'If Orange:
If Asheet.Range("b13").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g13") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g13").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b13").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e11") = "Yes" Then
Asheet.Range("g13") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b13").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e11") = "No" Then
Asheet.Range("g13") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 10
'If Orange:
If Asheet.Range("b14").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g14") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g14").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b14").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e12") = "Yes" Then
Asheet.Range("g14") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b14").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e12") = "No" Then
Asheet.Range("g14") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Individual 11
'If Orange:
If Asheet.Range("b15").Interior.Color = RGB(255, 153, 0) Then
Asheet.Range("g15") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("g15").Select
Selection.Font.Bold = True
End If
'If Blue & PJ = "Yes":
If Asheet.Range("b15").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e13") = "Yes" Then
Asheet.Range("g15") = "None."
End If
'If Blue & PJ = "No":
If Asheet.Range("b15").Interior.Color = RGB(153, 204, 255) And Dsheet.Range("e13") = "No" Then
Asheet.Range("g15") = "Notify RN, LPN, & Managment that prune juice has not been administered."
End If
'Data Validation for "Action Steps" Column
'Input Data Validation Criteria
Asheet.Range("o4") = "Action Steps Data Validation"
Asheet.Range("o5") = "Notify RN, LPN, & Managment that prune juice has not been administered."
Asheet.Range("o6") = "None."
Asheet.Range("o7") = "Notify RN, LPN, & Management of POTENTIAL HEALTH HAZARD!"
Asheet.Range("o8") = "See Comments Column."
'Data Validation
Range("g5:g15").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$O$5:$O$8"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
'Applying Borders to "Analysis" Tab
Asheet.Range("A4:h15").Select
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'Format Range e:5,h:15 as Wrapped Text
Asheet.Range("E4:h15").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.wraptext = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
End Sub