如何在Google表格(或Excel)中相对于换行符水平合并单元格?

时间:2017-10-10 23:35:37

标签: excel google-sheets

在电子表格中,我需要以ABAB形式合并单元格中的内容,其中每个单元格中的换行都受到尊重。我需要为1000行做这个,所以需要一个简单的批量程序。

我需要维护当前行,因为以后CSV将用于批量替换到字幕文件 - 一次1个当前行。一行很多继续包含与一个字幕文本字符串相关的所有数据。但是,我需要将中间和左侧列的内容组合起来:

--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

3 个答案:

答案 0 :(得分: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,然后使用填充柄将两个公式拖动到单元格W1

每个单元最多可处理10行。如果需要处理更多,请将助手公式拖到W1之前,并将单元C1中公式中的W1引用更新为拖动辅助公式的位置。 (每次在单元格C1中编辑数组公式时,请记住使用CTRL + SHIFT + ENTER进行保存。)

结果显示在单元格C1中。您需要手动将单元格C1的文本格式更改为Wrap Text以查看插入的换行符。

答案 1 :(得分: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

答案 2 :(得分:1)

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

对于Google表格。加入B12& C12