VBA格式化评论框

所以,我有下面的function,记住在一个评论框中,谁在单元格中进行更改的时间和date以及用户名,记住最后5个变化。 第六次更改时,删除最旧的一个,并打印最新的时间。 我也用代码格式化了评论框的格式。

Private Sub Worksheet_Change(ByVal Target As Range) Dim CommentBox As Object Dim CommentString As String Dim CommentTemp As String Dim LastDoubleDotPosition As Integer Dim LongestName As Integer Dim FinalComment As String If Range("A" & Target.Row).Value = "" Then GoTo EndeSub If Target.Row <= 2 Then GoTo EndeSub If Not Intersect(Range("C:JA"), Target) Is Nothing Then On Error GoTo EndeSub Application.EnableEvents = False Range("B" & Target.Row) = Now End If Application.Volatile Set CommentBox = Range("B" & Target.Row).Comment If Not CommentBox Is Nothing Then If CommentBox.Text <> "" Then CommentString = CommentBox.Text Range("B" & Target.Row).Comment.Delete End If Else CommentString = "" End If CommentTemp = CommentString LastDoubleDotPosition = 0 LongestName = 0 If InStr(CommentTemp, ":") > 0 Then StillTwoDoubleDots = True Do While InStr(CommentTemp, ":") > 0 If InStr(CommentTemp, ":") > LongestName Then LongestName = InStr(CommentTemp, ":") CommentTemp = Right(CommentTemp, Len(CommentTemp) - InStr(CommentTemp, ":")) Loop count = CountChr(CommentString, ":") If count >= 6 Then LastDoubleDotPosition = Len(CommentString) - Len(CommentTemp) - 1 CommentString = Left(CommentString, LastDoubleDotPosition - 13) End If 'insert comment FinalComment = Format(Now(), "DD.MM.YYYY hh:mm") & " " & "by" & " " & Application.UserName & vbCrLf & CommentString 'newComment and the oldcomment FinalComment = Replace(FinalComment, CustomComment, vbNullString) FinalComment = CustomComment & FinalComment Range("B" & Target.Row).AddComment FinalComment Set CommentBox = Range("B" & Target.Row).Comment LongestName = LongestName * 5 If LongestName < 150 Then LongestName = 150 With CommentBox .Shape.Height = 70 .Shape.Width = LongestName End With EndeSub: Application.EnableEvents = True End Sub Public Function CountChr(Expression As String, Character As String) As Long Dim Result As Long Dim Parts() As String Parts = Split(Expression, Character) Result = UBound(Parts, 1) If (Result = -1) Then Result = 0 End If CountChr = Result End Function 

你认为还可以为该评论框添加标题吗? 例如现在我有以下输出:

 13.11.2017 17:39 by user2 13.11.2017 17:35 by user1 13.11.2017 17:35 by user3 13.11.2017 17:34 by user1 13.11.2017 17:33 by user1 

我想添加一个大胆的标题,让我们说:“更新:”,和它的输出将是:

 Updated on: 13.11.2017 17:39 by user2 13.11.2017 17:35 by user1 13.11.2017 17:35 by user3 13.11.2017 17:34 by user1 13.11.2017 17:33 by user1 

像这样声明一个公共常量:

 Public Const UPDATED_ON = "UPDATED ON" & vbCrLf 

在最后写入注释时,尝试用下面的代码replaceUPDATED_ON的值:

 FinalComment = Replace(FinalComment, UPDATED_ON, vbNullString) 

最后在这个顶部添加UPDATED_ON:

 FinalComment = UPDATED_ON & FinalComment