use*_*539 6 excel vba excel-vba
我正在开发一个包含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
Run Code Online (Sandbox Code Playgroud)
这是必需的结果:
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
Run Code Online (Sandbox Code Playgroud)
我在论坛上搜索了类似的代码,但是我无法根据自己的目的进行调整.
更新:不确定为什么关闭它,假设你可能想要一些代码的例子.我正在使用下面的宏,我在互联网上找到:
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
Run Code Online (Sandbox Code Playgroud)
但它不起作用,也许我已经错误地调整了它.
Kaz*_*wor 13
试试这段代码:
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
Run Code Online (Sandbox Code Playgroud)
之前 ----------------------------------------- 之后
