[英]Excel VBA Comment Query
因此,我多次尝试并失败了。
我正在尝试编写一个VBA脚本,该脚本在运行时将在活动单元格中插入一条注释,内容为:
[姓氏],[名字] [今天的日期]为[当前单元格值]
姓和名应为粗体。
如果再次编辑该单元格,它将编辑注释并将上述格式放在前一个注释下方。
这是我目前所拥有的(记住这可能是非常错误的)
Sub Test()
Dim UserN As String
Dim commt As String
Dim cmt As Comment
UserN = Application.UserName
Set cmt = ActiveCell.Comment
If ActiveCell.Comment Is Nothing Then
commt = UserN & Chr(10) & Chr(10) _
& Chr(10) & Format(Now, strDate) & " was £" & ActiveCell
With Selection
With Cells(Selection.Row, Selection.Column)
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Shape.AutoShapeType = msoShapeRoundedRectangle
.Comment.Text Text:=commt
With .Comment.Shape.TextFrame
' Username
With .Characters(1, Len(UserN)).Font
.Bold = True
End With
With .Characters.Font
.Size = 12
End With
End With
.Comment.Shape.TextFrame.AutoSize = True
End With
End With
Else
commt.txt = commt.txt & Chr(10) UserN & Chr(10) & Chr(10)
& Chr(10) & Format(Now, strDate) & " was £" & ActiveCell
With Selection
With Cells(Selection.Row, Selection.Column)
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Shape.AutoShapeType = msoShapeRoundedRectangle
.Comment.Text Text:=commt
With .Comment.Shape.TextFrame
' Username
With .Characters(1, Len(UserN)).Font
.Bold = True
End With
With .Characters.Font
.Size = 12
End With
End With
.Comment.Shape.TextFrame.AutoSize = True
End With
End With
End If
End Sub
我认为唯一的主要问题是与此行:
commt.txt = commt.txt & Chr(10) UserN & Chr(10) & Chr(10)
& Chr(10) & Format(Now, strDate) & " was £" & ActiveCell
它使用变量“ commt”,就好像它是一个注释对象一样,只是一个字符串。 它还缺少“&”和“ _”行分隔符。 我相信这行应该是:
commt = cmt.text & Chr(10) & UserN & Chr(10) & Chr(10) & Chr(10) & Format(Now, strDate) & " was £" & ActiveCell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.