Você está na página 1de 1

Combining Columns

There may be times when you have a need to concatenate cells together. For instance, you
may have information in three columns, but you want it combined together into the first
column of each row. The following macro, StuffTogether, will do just that. It examines the
range of cells you select, and then moves everything from each cell in a row into the first
cell of the row.

Sub StuffTogether()
Dim FirstCol As Integer, FirstRow As Integer
Dim ColCount As Integer, RowCount As Integer
Dim ThisCol As Integer, ThisRow As Integer
Dim J As Integer, K As Integer
Dim MyText As String

FirstCol = ActiveWindow.RangeSelection.Column
FirstRow = ActiveWindow.RangeSelection.Row
ColCount = ActiveWindow.Selection.Columns.Count
RowCount = ActiveWindow.Selection.Rows.Count

For J = 1 To RowCount
ThisRow = FirstRow + J - 1
MyText = ""
For K = 1 To ColCount
ThisCol = FirstCol + K - 1
MyText = MyText & Cells(ThisRow, ThisCol).Text & " "
Cells(ThisRow, ThisCol).Value = ""
Next K
MyText = Trim(MyText)
Cells(ThisRow, FirstCol).Value = MyText
Next J
End Sub

Você também pode gostar