我有一个带有格式化文本的单元格,其中包含我想要用行更改替换的某个子字符串。子字符串是带有方括号的 [enterkey]。这是这个问题的一个变体在 Excel 中将 HTML 标记 () 替换为 Alt+Enter https://stackoverflow.com/questions/17874391/replace-html-tag-br-with-altenter-in-excel但我想在更改行后继续格式化。
我使用的代码是一个简单的替换,其中 rng2 是输入和输出单元格:
rng2.Value = Replace(rng2.Value, "[enterkey]", Chr(10))
我的问题
如何防止该代码在插入换行符时复制上面行的格式?我希望它保留原样的格式并简单地插入换行符,如图所示。
更多信息
我实际上正在转换为 HTML 并返回。我不想添加不必要的详细信息,但如果人们真的想知道,那是因为我将值存储在 Access 数据库中,并且我想保留单元格格式。从 Access 检索信息时,我想再次采用正确的格式。这个问题只涉及转换回来。有关完整详细信息,请参阅下图。
经过一番研究,我得到了一些有用的东西。
这段代码:
Sub ReplaceEnterKey()
Dim rng2 As Range
Set rng2 = Range("O15")
Dim iChr As Integer
iChr = InStr(1, rng2.Value, "[e") 'find beginning of first occurence of "[enterkey]"
Do Until iChr = 0 'loop until no more occurences found
rng2.Characters(iChr, 10).Delete 'remove found occurrence of "[enterkey]" - 10 characters
rng2.Characters(iChr, 0).Insert Chr(10) 'insert carriage return where "[enterkey]" used to be
iChr = InStr(1, rng2.Value, "[e") 'look for next occurence
Loop
End Sub
创造了这个结果。N15
是原创的O15
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)