2017-10-10 118 views
-1

在電子表格中,我需要合併ABAB表單中單元格內的內容,其中每個單元格內的換行符都受到尊重。我需要爲1000線做這個,所以需要一個簡單的批量程序。如何在Google表格(或Excel)中水平地對單元格進行合併?

我需要維護當前行,因爲以後CSV將用於批量替換到字幕文件 - 一次一個當前行。很多行繼續包含與一個字幕文本字符串相關的所有數據。不過,我需要得到中間和左側欄的內容從組合:

--cell one--  --cell two-- 

A1     B1 

A2     B2 

A3     B3 

--cell three--  --cell four-- 

C1     D1 

C2     D2 

C3     D3 

成所需的結果形式:

--cell one--  

A1(B1) 

A2(B2) 

A3(B3) 

--cell three--  

C1(D1)..... etc.... 
換句話說,這一切都在一個小區

所以:

niemniej (still) 
jednak (but) 
zgodził (he agreed) 
się udzielić (to grant) 
Kate (kate) 
wywiadu. (interview) 

google sheets translation word for word - SCREENSHOT

+0

VBA:使用'Split(cellText,vbLf)'從兩個要連接的單元中的每一個單元創建數組。將兩個數組「壓縮」在一起循環並連接文本,然後合併單元格並用「壓縮」版本替換文本。 –

回答

1
=ARRAYFORMULA(JOIN(CHAR(10),SPLIT(B12,CHAR(10))&"("&SPLIT(C12,CHAR(10))&")")) 

對於谷歌片在相應的值。加入B12 & C12

+1

難以置信的簡單,我花了很多時間在錯誤的地方尋找一種方法!謝謝 –

1

它需要一些輔助列,但這裏有一個沒有VBA的解決方案。這要求您的數據從單元格A1開始,並且還要求每個單元格中的每行都是唯一的。它還要求Excel 2016或更高版本使用CONCAT功能。對小區C1

陣列式(必須使用CTRL + SHIFT + ENTER被保存):

=LEFT(CONCAT(OFFSET(D1,0,0,1,MATCH(TRUE,ISERROR(D1:W1),0)-1)),LEN(CONCAT(OFFSET(D1,0,0,1,MATCH(TRUE,ISERROR(D1:W1),0)-1)))-1)

公式細胞D1:

=LEFT(A1,FIND(CHAR(10),A1)-1)

公式細胞E1:

="("&LEFT(B1,FIND(CHAR(10),B1)-1)&")"&CHAR(10)

公式單元格F1:

=IFERROR(MID($A1,FIND(D1,$A1)+LEN(D1)+1,FIND(CHAR(10),$A1,FIND(D1,$A1)+LEN(D1)+1)-(FIND(D1,$A1)+LEN(D1)+1)),RIGHT($A1,LEN($A1)-(FIND(D1,$A1)+LEN(D1))))

公式單元格G1:

="("&IFERROR(MID($B1,FIND(MID(E1,2,LEN(E1)-3),$B1)+LEN(MID(E1,2,LEN(E1)-3))+1,FIND(CHAR(10),$B1,FIND(MID(E1,2,LEN(E1)-3),$B1)+LEN(MID(E1,2,LEN(E1)-3))+1)-(FIND(MID(E1,2,LEN(E1)-3),$B1)+LEN(MID(E1,2,LEN(E1)-3))+1)),RIGHT($B1,LEN($B1)-(FIND(MID(E1,2,LEN(E1)-3),$B1)+LEN(MID(E1,2,LEN(E1)-3)))))&")"&CHAR(10)

選擇兩個單元F1:G1,然後用填充柄兩個公式拖到cell W1

這將處理每個單元最多10行。如果需要處理更多,請將助手公式拖到W1以上,並將單元格C1中公式中的W1的引用更新爲拖動助手公式的位置。 (請記住,在每次編輯單元格C1中的數組公式時使用CTRL + SHIFT + ENTER進行保存。)

結果顯示在單元格C1中。您需要手動將單元格C1的文本格式更改爲Wrap Text以查看插入的換行符。

1

我試圖字符串以定界符作爲換行符(CHR(13))兩者的列

分裂然後我級聯每個陣列

Sub splitandconcatenate() 
Dim s1() As String, l1() As String 
Dim rowcount As Integer, currentRow As Integer 
Dim i As Integer 
Dim lookupRowValue As String, sourceRowvalue As String 
Dim sourceCol As Integer, CheckCol As Integer 

sourceCol = 1 ' 1 denotes Column A. Data in A column 
CheckCol = 2 ' 2 denotes Column B. Data in B column 
TargetCol = 3# ' 3 output is written in column c 
rowcount = Cells(Rows.Count, sourceCol).End(xlUp).Row 'counts the rows with data 

'For each row 
For currentRow = 1 To rowcount 
    sourceRowvalue = Cells(currentRow, sourceCol).Value 
    lookupRowValue = Cells(currentRow, CheckCol).Value 
'Split each cell with delimiter being newline chr(13) 
    s1 = Split(Chr(13) & sourceRowvalue, Chr(13)) 
    l1 = Split(Chr(13) & lookupRowValue, Chr(13)) 
'Two arrays are created for two columns. For each string in the array, concatenate the corresponding string in the other array 
     For i = 1 To UBound(s1) 
     Cells(currentRow, TargetCol).Value = Cells(currentRow, TargetCol).Value & Chr(13) & s1(i) & "(" & l1(i) & ")" 

     Next 
Next 
End Sub