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