简体   繁体   English

在已经包含文本的单元格中的光标处插入文本(MS Excel VBA 2013)

[英]Insert text, at the cursor, in a cell that already contains text (MS Excel VBA 2013)

For the record I have very limited experience with coding. 出于记录,我在编码方面的经验非常有限。 We use Excel to document all the real time events in the day. 我们使用Excel记录当天的所有实时事件。 Reports are approx 100 to 150 rows long with multiple lines in each row. 报告长约100至150行,每行多行。 Each row uses 12 merged cells and text is wrapped. 每行使用12个合并的单元格并包装文本。

Desired Action: to insert specific text, after the cursor, in a cell that already contains text (MS 2013 Excel using VB). 期望中的动作:插入特定文本,光标后,在单元格中已包含文本(MS 2013 Excel中使用VB)。

The code below works except it puts the text at the top of the cell instead of at the cursor. 下面的代码有效,除了将文本放在单元格的顶部而不是光标处。 We cannot use a text box and recording doesn't work. 我们无法使用文本框,并且无法录制。 What code can accomplish this? 哪些代码可以完成此任务? Thank you! 谢谢!

Sub SuziQueue () 

    With ActiveCell 
        .Value = "<Suzi Queue... >" & .Value 
    End With 

End Sub 

Maybe this example will help 也许这个例子会有所帮助

  • if you have the text "Test String" in ActiveCell 如果你有ActiveCell文本“测试字符串”

  • you want to insert the word "New" in between the 2 words 您想要在两个单词之间插入单词“ New”

.

Option Explicit

Sub insertText()
    Dim indx As Long, lastPart As String

    With ActiveCell
        MsgBox .Value2      'shows "Test String"

        indx = InStr(.Value2, "S")
        lastPart = Right(.Value2, indx + 1)

        .Characters(indx).Insert "New" & lastPart 'inserts "New" between "Test" & "String"

        MsgBox .Value2      'shows "Test New String"
    End With
End Sub

.

There might be ways to determine the cursor position in the cell using APIs 可能存在使用API​​确定单元格中光标位置的方法

This Microsoft link shows how to get the cursor position on the screen 此Microsoft链接显示了如何让屏幕上的光标位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM