vba 从文本范围的末尾删除单个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17752649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete a single character from the end of a range of text
提问by lucas.r
I am trying to use VBA code to delete the last character (",") off of a specific range.
It gives me a compile error saying "Expected: =" when I enter the code I have.
我正在尝试使用 VBA 代码删除特定范围外的最后一个字符 (",")。
当我输入我拥有的代码时,它给了我一个编译错误说“预期:=”。
Here is my line of code that is giving me problems.
这是给我带来问题的代码行。
doc.Bookmarks("F1").Range.Delete(wdCharacter,-1)
回答by Floris
The following code appears to do what you want:
以下代码似乎可以执行您想要的操作:
Option Explicit
Sub test()
Dim r
If ActiveDocument.Bookmarks.Exists("mark1") Then
r = ActiveDocument.Bookmarks("mark1").Range.End
ActiveDocument.Range(r - 1, r).Delete
End If
End Sub
Explanation:
解释:
- Always declare your variables;
Option Explicit
will complain if you forget / misspell - Check the bookmark exists
- Find the position of the last character of the bookmark
- Delete the range of characters starting one from the end
- 始终声明您的变量;
Option Explicit
如果你忘记/拼错会抱怨 - 检查书签是否存在
- 查找书签最后一个字符的位置
- 删除从末尾开始的字符范围
回答by user2600744
Try this way of deleting last character of your bookmark:
试试这种删除书签最后一个字符的方法:
doc.Range(doc.Bookmarks("F1").End - 1, _
doc.Bookmarks("f1").End).Delete