gpt4 book ai didi

excel - 在换行符处拆分单元格中的文本

转载 作者:行者123 更新时间:2023-12-02 03:14:00 29 4
gpt4 key购买 nike

我正在处理一个包含 39 列数据的 Excel 电子表格。其中一列(AJ 列)是描述字段,包含详细描述行项目的文本。单元格内的文本有时会超过一行,并且可以通过按 (ALT+Enter) 开始新行。

我需要能够复制整个工作表并将其全部放入另一个工作表(现有工作表)中,但为 AJ 列中的每个新添加一个新行,如下所示:

Column A     Column B     Column AJ
Electrical Lighting This is line one of the text
And in the same cell on a new line

这是所需的结果:

Column A     Column B     Column AJ
Electrical Lighting This is line one of the text
Electrical Lighting And in the same cell on a new line

我在论坛中搜索了类似的代码,但我在根据自己的目的调整它时遇到了困难。

更新:不确定为什么它被关闭,假设您可能需要一些代码的示例。我使用的是在互联网上找到的以下宏:

Sub Splt()
Dim LR As Long, i As Long
Dim X As Variant
Application.ScreenUpdating = False
LR = Range("AJ" & Rows.Count).End(xlUp).Row
Columns("AJ").Insert
For i = LR To 1 Step -1
With Range("B" & i)
If InStr(.Value, ",") = 0 Then
.Offset(, -1).Value = .Value
Else
X = Split(.Value, ",")
.Offset(1).Resize(UBound(X)).EntireRow.Insert
.Offset(, -1).Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
End If
End With
Next i
Columns("AK").Delete
LR = Range("AJ" & Rows.Count).End(xlUp).Row
With Range("AJ1:AK" & LR)
On Error Resume Next
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
On Error GoTo 0
.Value = .Value
End With
Application.ScreenUpdating = True
End Sub

但是它不起作用,也许我调整得不正确。

最佳答案

尝试使用以下代码:

Sub JustDoIt()
'working for active sheet
'copy to the end of sheets collection
ActiveSheet.Copy after:=Sheets(Sheets.Count)
Dim tmpArr As Variant
Dim Cell As Range
For Each Cell In Range("AJ1", Range("AJ2").End(xlDown))
If InStr(1, Cell, Chr(10)) <> 0 Then
tmpArr = Split(Cell, Chr(10))

Cell.EntireRow.Copy
Cell.Offset(1, 0).Resize(UBound(tmpArr), 1). _
EntireRow.Insert xlShiftDown

Cell.Resize(UBound(tmpArr) + 1, 1) = Application.Transpose(tmpArr)
End If
Next
Application.CutCopyMode = False
End Sub

之前---------------------------------------------------- --之后

enter image description here enter image description here

关于excel - 在换行符处拆分单元格中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19851951/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com