vba - 保护工作表时出现 VBA 运行时错误 1004
问题描述
以下代码旨在在受保护的工作表上运行,但是当我在运行代码后重新保护它时,在具有 Range 或 Cell 的代码行上出现错误 1004“应用程序定义或对象定义错误”。如果我之后不重新保护工作表,我不会收到此错误。有什么帮助吗?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells, KeyCells2 As Range
With Sheets(2)
.Unprotect Password:="xyz"
Set KeyCells = .Range("U5")
If Not Application.Intersect(KeyCells, .Range(Target.Address)) Is Nothing Then
lastrow = FindLastRow("*")
.Range("T8:T" & lastrow) = .Range("U5")
For i = 8 To lastrow
If .Cells(i, "R") > .Cells(i, "T") Then
.Cells(i, "U").Value2 = IIf(.Cells(i, "R") > .Cells(i, "S"), Cells(i, "R"), Cells(i, "S"))
Else
.Cells(i, "U").Value2 = IIf(.Cells(i, "T") > .Cells(i, "S"), .Cells(i, "T"), Cells(i, "S"))
End If
Next i
End If
End With
Call PS3.ProtectSheet
End Sub
解决方案
推荐阅读
- python - 将不同的东西分配给由类制成的对象的最有效方法?
- c# - 使用 lambda 表达式计算表中的行数
- vue.js - Vue 3 和 webpack 5 - 错误:“模块属性已从依赖项中删除”
- r - 如何省略负数的条目?
- javascript - Vue 组件不会使用 getter 数据重新加载(非响应式)
- android - 如何在 AndroidManifest.xml 中为 Android Deeplinking 注入主机和架构
- java - 索引大文本文件的最快方法
- javascript - CSS 更改时如何在不重新加载的情况下更新 JavaScript?
- firebase - 文本注释应用程序的 Cloud Firestore 架构
- amazon-web-services - FilterExpression 仅适用于 DynamoDB 查询中的比较器“=”