excel - 在 Excel 中使用 VBA 向单元格添加注释时出现运行时错误 1004
问题描述
我收到 Excel VBA 运行时错误 1004:应用程序定义或对象定义错误,同时使用 VBA 向 Excel 单元格范围添加注释。
评论文本来自用户表单:
Description = TextBox1.Value
StartTime = TextBox2.Value
EndTime = TextBox3.Value
InputText = StartTime & " - " & EndTime & " " & Description
MsgBox (InputText)
这部分效果很好。之后有一些代码来格式化单元格。最后 VBA 应该为每个单元格添加注释。
Dim Cell As Range
For Each Cell In Selection
Cell.AddComment
Cell.Comment.Visible = False
Cell.Comment.Text Text:=InputText **'// ERRORLINE//**
Next Cell
我已经尝试更改一些代码,但没有任何运气:
Dim Cell As Range
For Each Cell In Selection
'Cell.Comment.Delete
Set Comment = Cell.Comment
Cell.Comment.Visible = False
Cell.Comment.Text Text:=InputText
Next Cell
没有任何问题的工作是:
Dim Cell As Range
For Each Cell In Selection
'Cell.Comment.Delete
Set Comment = Cell.Comment
Cell.Comment.Visible = False
Cell.Comment.Text Text:="InputText"
Next Cell
是什么导致了这个错误?
解决方案
在添加新评论之前清除评论:
For Each cell In Selection
With cell
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Text Text:=InputText
End With
Next cell
推荐阅读
- powershell - 在 Powershell 中自动将数字添加到变量中
- javascript - Javascript setAttribute 无法正常工作
- java - Java Spring 枚举 Jackson Serializer 用于数据插入/获取
- php - PHP - 验证日期格式为 d/m/Y
- visual-studio - 有没有办法通过 Visual Studio 菜单/工具栏优雅地关闭正在运行的应用程序?
- ionic-framework - ion-textarea 将 ion-header 一直推到顶部并将其隐藏
- python - 如何从文件名中删除 BOM 字符?
- c++ - 在 Windows 主页上 Dockerize 一个 Visual Studio C++ 项目
- c - 分配给作为指针的结构组件
- javascript - 在 Ruby on Rails 应用程序中使用 JSON 将 Googlesheets 中的数据解析为 FusionCharts 时未定义的局部变量或方法“parsedData”