excel - 带有 activex 控件复选框和自定义函数的 Excel 宏显示“#VALUE!” 错误
问题描述
我只是 excel 和 vb 脚本的新手,并且添加了带有自定义功能的 ActiveX 复选框控件,如果选中复选框,则为行着色,如果未选中,则将其删除。
宏代码:
Public Sub GenerateCheckBox()
Dim i As Integer
Dim linkedcell
linkedcell = "B"
initialTop = 15
totalRow = (Cells(Rows.Count, 4).End(xlUp).row - 1)
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Dim checkbox As OLEObject
Range("A:C").Insert
Range("A1").Value = "Resolved"
For i = 1 To totalRow
activeRow = i
Set checkbox = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1", Link:=False, _
DisplayAsIcon:=False, Left:=0, Top:=initialTop, Width:=13, Height:=15)
checkbox.linkedcell = linkedcell & activeRow
checkbox.Object.Caption = ""
checkbox.Object.BackStyle = 0
checkbox.Object.Value = False
initialTop = initialTop + 15
ws.Range("C" & activeRow).Formula = "=ColorRowIF(B" & activeRow & "=True)"
Next i
End Sub
Public Function ColorRowIF(Condition As Boolean, Optional r As Integer = 100, Optional g As Integer = 255, Optional b As Integer = 4) As String
Dim row As Integer
row = Application.Caller.row
If Condition = True Then
' Paints the checked row with color
ActiveSheet.Evaluate "ColorRow(" & row & ", " & r & ", " & g & ", " & b & ")"
Else
'Removes the color from the unchecked row
ActiveSheet.Evaluate "RemoveRowColor(" & row & ")"
End If
ColorRowIF = Condition
End Function
Sub DeleteAll()
Dim obj As OLEObject
For Each obj In ActiveSheet.OLEObjects
obj.Delete
Next obj
Columns("A:C").Delete
End Sub
此代码完美运行,直到行数超过2000 行。在 2000 行之后,C 列返回“#VALUE!” 所有现有单元格的错误,并且不评估结果。
此外,我还添加了一种删除 ActiveX 复选框和前 3 列的方法,这对于添加了复选框和公式的 2000 行也非常有效。
之后VB抛出以下错误:
运行时错误“-2147319765 (8002802b)”:
找不到自动化错误元素
当我单击调试选项时,光标将For Each obj In ActiveSheet.OLEObjects
方法显示为有错误。
解决方案
推荐阅读
- jenkins - 如何在没有构建排队的情况下使用管道有效地运行并行作业
- arrays - json数组字符值升序降序打字稿
- python - 如何处理`关系“cms_disclaimerpanel”已经存在`和ProgrammingError:关系“xyz”的列“http_request_lang”不存在
- python - 如何检查列表中是否存在数据,我的列表有多种数据类型?
- sql-server - 如果出现问题,如何在程序中的循环中删除和重新创建临时表怎么办?
- python - 如何从外部导入模块
- php - 从存储文件夹访问图像
- c# - 从 Selenium docker 容器访问 IIS 本地主机
- javascript - 如何使用文件阅读器从 Angular 中的文件制作 SHA1?
- python - 如何将 python post 请求转换为 Postman POST 请求