Macro to join two Excel columns

Asked

Viewed 357 times

0

Hello! I have a question about Excel. I have two columns, I would like a macro that when used, transforms all the rows of these two columns into rows of only one column. I know I can use the concatenate command, but I only know to do it in one line at a time, my document is huge and it becomes impossible to do it manually. Thanks in advance!

1 answer

0

Good morning, follow the code. Just select the first row of the columns you want to concatenate. Run with two columns only.

inserir a descrição da imagem aqui

Option Explicit

Sub concatena_colunas()
    Dim coluna_01 As Long
    Dim coluna_02 As Long
    Dim linha As Long

    On Error GoTo sair:
    'Armazena e coloca em ordem crescente
    coluna_01 = Selection.Areas(1).Column
    If Selection.Areas(2).Column < coluna_01 Then
        coluna_02 = Selection.Areas(1).Column
        coluna_01 = Selection.Areas(2).Column
    Else
        coluna_02 = Selection.Areas(2).Column
    End If

    linha = ActiveCell.Row

    Do
        Cells(linha, coluna_01).Value = Cells(linha, coluna_01).Value & " " & Cells(linha, coluna_02).Text
        linha = linha + 1
    Loop While Cells(linha, coluna_01).Value <> Empty

    coluna_01 = 0
    coluna_02 = 0
    linha = 0
sair:
End Sub
  • Unfortunately the code did not work for me, I modified the values there at the end of the coluna_01 and coluna_02 and line for the ones I use, but nothing happened. I must change something else?

  • No need to change the code, just select the first row of each column you want to concatenate. I edited the answer by adding a figure.

  • Thank you! I managed to make it work, but unfortunately I’m trying to join two columns, a date and a time, the date is copied without problem, but the time is all disfigured, even changing the type of column to date+time. Got any tips on how to fix this?

  • I changed the code, see if it works, here it worked.

  • 1

    It worked perfectly, thank you so much for your help !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.