将行复制到一个单元格VBA中 [英] Copying rows into one cell VBA

查看:184
本文介绍了将行复制到一个单元格VBA中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作表,其中只有一列如下:

I've a worksheet with various values in only one column like:

A
B
C
D
E
F
...



而我需要编写一个简单的代码,将其全部放在一个单元格(例如D2)中,并且该单元格我想要有一个值:

And I need a write a simple code that will put this all together in one cell (for example D2) and that cell I want to have a value:

A;B;C;D;E;F;...

我觉得这很简单,但是我太生病了,可以帮忙吗? :(

I think it's quite easy, but I'm too ill to think about it, would you help? :(

推荐答案

以简单的形式,以下VBA宏可以做这项工作(假设你在Excel Worksheet的列A中有值你想连接并将结果放在Cell D2中):

In a simple form, the following VBA macro can do the job (assuming you have values in Excel Worksheet's Column A that you want to concatenate and place a result in the Cell D2):

Sub ConcatenateCells()
Dim i As Integer
Dim count As Integer
Dim s As String
count = Cells(Rows.count, "A").End(xlUp).Row
For i = 1 To count
    s = s & Cells(i, 1) & ";"
Next
Range("D2") = s
End Sub

您可以为此目的修改它,此外,您可以通过添加条件排除空单元格:

You can modify it for your purpose. Also, you may exclude the empty cells by adding the condition:

If Cells(i, 1).Value <> "" Then s = s & Cells(i, 1).Value & ";"

希望这可能有所帮助。最好的问候,

Hope this may help. Best regards,

这篇关于将行复制到一个单元格VBA中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆