查找换行符并添加新行的 Excel VBA 宏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23156256/
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
Excel VBA macro that finds newline and adds a new row?
提问by user2166059
Selection.Replace What:="" & Chr(10) & "", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
I have a spreadsheet with multiple cells that contain multiple newlines ( the little question mark box symbol). What I'm trying to do is find all the newlines add a new row for each newline found and paste the content after the newline into the new row. I'm new to macros so I tried recording one to try to understand it. Right now the above code is finding the new line and replacing it with a space in the same cell. Not sure how to go about adding the new row?
我有一个包含多个单元格的电子表格,其中包含多个换行符(小问号框符号)。我想要做的是找到所有换行符为找到的每个换行符添加一个新行,并将换行符后的内容粘贴到新行中。我是宏的新手,所以我尝试录制一个宏来尝试理解它。现在上面的代码正在查找新行并将其替换为同一单元格中的空格。不确定如何添加新行?
回答by Gary's Student
This is a partial answer. It is for a single cell
这是部分答案。它是针对单个单元格的
Select a cell containing text with included hard returns and run:
选择一个包含硬回车文本的单元格并运行:
Sub dural()
Dim r As Range, s As String, HR As String
Set r = Selection(1)
v = r.Value
HR = Chr(10)
If InStr(v, HR) = 0 Then Exit Sub
ary = Split(v, HR)
For i = 1 To UBound(ary)
r.Offset(1, 0).EntireRow.Insert
Next i
For i = 0 To UBound(ary)
r.Offset(i, 0).Value = ary(i)
Next i
End Sub
You should embed this in loop(s) to cover all the cells in question.
您应该将其嵌入循环中以覆盖所有相关单元格。