我想连接列,但是在VBA的第一列中,就像那样:
A | B | C |
sentence1 | sentence2 | sentence3 |
sentence4 | sentence5 | sentence6 |
sentence7 | sentence8 | sentence9 |
- >
A | B | C
sentence1 sentence2 sentence3 | nothing | nothing
sentence4 sentence5 sentence6 | nothing | nothing
sentence7 sentence8 sentence9 | nothing | nothing
我该怎么办? 提前谢谢!
答案 0 :(得分:1)
Dim tempval As String
Dim row As Integer, col As Integer
Application.ScreenUpdating = False
'loop through rows
For row = 1 To 3 Step 1
'clear temp string
tempval = ""
'loop through columns
For col = 1 To 3 Step 1
'save columnvalues in temp
tempval = tempval & Cells(row, col).Value
'delete cell value
Cells(row, col).Value = ""
Next col
'paste saved string into first cell
Cells(row, 1).Value = tempval
Next row
Application.ScreenUpdating = True
答案 1 :(得分:1)
以下是您所要求的,并且更为通用:
它考虑了列#34; A"的所有单元格。里面有一些文字
它扩展了其内容将连接到给定行中所有连续非空白单元格的范围
换句话说,这种方法既不会受到任何可能的列数变化的影响(它们可以是3,根据您的示例,或者更多或更少),也不会受到所有行具有相同数量的细胞填充
Option Explicit
Sub main()
Dim cell As Range
With Worksheets("mySheet").Columns("A").SpecialCells(xlCellTypeConstants, xlTextValues)
For Each cell In .Cells
cell.Value = Join(Application.Transpose(Application.Transpose(Range(cell, cell.End(xlToRight)))))
Range(cell.Offset(, 1), cell.End(xlToRight)).Clear
Next cell
.WrapText = False
.EntireColumn.AutoFit
End With
End Sub
答案 2 :(得分:0)
Dim r As Range
For Each r In Sheet1.UsedRange
r(1, 1).Value = r(1, 1).Value & " " & r(1, 2).Value & " " & r(1, 3).Value
r(1, 2).Value = ""
r(1, 3).Value = ""
Next r