[英]Merge text from two cells in Excel into one with VBA
asuming A
是主要的(和空双打)
asuming H
持有文本
然后在L1
复制下来
=H1&IF(LEN(A2),H2,"")
最简单的方法(然后将值从L
复制到H
并删除空行(只需使用过滤器)
当拥有未知行数(分裂后)时,你最好使用vba(或者只是重复整个过程直到没有空行...
在VBA中这样做:
Sub testing()
Dim i As Long
While Len(Cells(i + 1, 8))
i = i + 1
While Len(Cells(i + 1, 1)) = 0 And Len(Cells(i + 1, 8))
Cells(i, 8) = Cells(i, 8) & Cells(i + 1, 8)
Rows(i + 1).Delete
Wend
Wend
End Sub
大多数程序跳过空格,所以你可能想要使用:
=H1&IF(LEN(A2)," "&H2,"")
或者对于vba改变Cells(i, 8) = Cells(i, 8) & Cells(i + 1, 8)
到Cells(i, 8) = Cells(i, 8) & " " & Cells(i + 1, 8)
这将连接H中的文本并删除无用的行:
Sub test_bm11()
Dim wS As Worksheet, _
LastRow As Long, _
i As Long
Set wS = ActiveSheet
With wS
LastRow = .Range("H" & .Rows.Count).End(xlUp).Row
For i = LastRow To 2 Step -1
If .Cells(i, "A") <> vbNullString Then
Else
.Cells(i, "H").Offset(-1, 0) = .Cells(i, "H").Offset(-1, 0) & .Cells(i, "H")
.Cells(i, "H").EntireRow.Delete
End If
Next i
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.