Excel VBA Formating在插入字符时丢失了

我有以下代码处理我的列之一的双击事件。 基本上它是一个备注列,所以当用户双击它时,它popup一个input并提示input。 VBA代码然后附加一个date并将其插入到单元格中。 我想让date变得粗暴。

但是,当我第一次input评论时,单元格是正确的。 喜欢这个

23/08/2013:你好

当我再次双击单元格并input“Hi”时,整个单元格变为粗体

23/08/2013:你好

23/08/2013:嗨,再次

我认为这是因为我重置整个单元格文本,而不是附加到原始文本..因此失去了原始格式。

任何人都可以摆脱这个任何想法。 唯一能算出来的方法就是可以通过查找并findctrl(10)char,然后将其格式化,然后将其格式化。

关于D

Option Explicit Const STATUS_COL As Integer = 10 Const NOTES_COL As Integer = 13 Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim newVal As String Dim strNote As String Dim lngPos As Long If Target.Count > 1 Then GoTo exitHandler Application.EnableEvents = False On Error Resume Next If Target.Column = NOTES_COL Then 'Add a note lngPos = Len(Target.Value) strNote = InputBox(Prompt:="Enter Note", _ Title:="Notes", Default:="") If (Len(Trim(strNote)) > 0) Then If Target.Value = "" Then newVal = Date & ": " & strNote Else newVal = Target.Value + Chr(10) & Date & ": " & strNote End If Target.Value = newVal 'set the new value Target.Characters(Start:=lngPos + 1, Length:=11).Font.Bold = True End If End If exitHandler: Application.EnableEvents = True End Sub 

 Sub tester() AddNote ActiveSheet.Range("A1"), "hello" AddNote ActiveSheet.Range("A1"), "world" End Sub Sub AddNote(rng As Range, txt As String) Dim l As Long, s As String l = Len(rng.Value) s = IIf(l > 0, Chr(10), "") & Format(Date, "mm/dd/yyyy") & ": " & txt rng.Characters(l + 1, Len(s)).Text = s rng.Characters(l + 1, 11).Font.Bold = True End Sub