[英]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.