EXCEL VBA代码?

从C3开始和C4,这2个单元格合并起来,2个单元格的数据用 换行符 连接起来.然后下面的单元格清空.效果就是E列的这样. C列里需要操作的区域之间有固定的10行相隔.请问这个代码应该怎么写啊?

我的程序不是固定10行空白,是自动跳过空白,示例数据:

执行后的效果:

程序窗口:

程序文本:

Sub 合并C列()

    Dim i&, i1&, i2&, j&, s$

    j = 3 'C列

    i1 = Cells(Rows.Count, j).End(xlUp).Row '最后一行

    While i1 > 1

        i2 = Cells(i1, j).End(xlUp).Row '本组开始行

        s = ""

        For i = i2 To i1

            s = s & Chr(10) & Cells(i, j)

        Next i

        s = Right(s, Len(s) - 1)

        With Range(Cells(i2, j), Cells(i1, j))

            .ClearContents

            .Merge

        End With

        With Cells(i2, j)

            .Value = s

            .WrapText = True

        End With

        i1 = Cells(i2, j).End(xlUp).Row '上一行

    Wend

End Sub

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-10-22

代码如下:

Sub Combine()

For i = 1 To 200  '可根据实际情况调整

If Range("C" & 3 + (i - 1) * 12) = "" Then Exit For

If Range("C" & 3 + (i - 1) * 12) = "实际" Then Range("C" & 3 + (i - 1) * 12) = Range("C" & 3 + (i - 1) * 12) & Chr(10) & Range("C" & 3 + (i - 1) * 12 + 1): Range("C" & 3 + (i - 1) * 12 + 1) = ""

Next

End Sub

循环次数200根据“实际”有多少个进行设置。

效果:

以上希望能帮上忙!

本回答被提问者采纳
第2个回答  2019-10-22
你给的图上相隔是11行哦,有一个合并的行
sub com()
dim i&,j&,rr&
rr=[C65536].end(xlup).row
for i=3 to rr step 12
cells(i,3)=cells(i,3).value & char(13) & cells(i+1,3).value
cells(i+1,3)=""
next
end sub
第3个回答  2019-10-22
Sub mm()
Dim i%, r%
r = Range("c65536").End(xlUp).Row
For i = 3 To r Step 12
Cells(i, 3) = Cells(i, 3) & Chr(10) & Cells(i + 1, 3)
Cells(i + 1, 3).ClearContents
Next
End Sub
相似回答