Copying rows into one cell VBA [duplicate]

耗尽温柔 提交于 2020-01-03 05:32:27

问题


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

A
B
C
D
E
F
...

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? :(


回答1:


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,




回答2:


If you had Googled, you might stumble upon this link, which is quite funky way of doing it using Excel formula..

=CONCATENATE(TRANSPOSE(A1:A6 & ";"))

You just need to make sure that TRANSPOSE function is executed using F9 prior.

However you could always use VBA User Defined Function to get it done too.



来源:https://stackoverflow.com/questions/30594931/copying-rows-into-one-cell-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!