macro to copy and paste

4

I have data in the "M" column, which are automatically entered in an average of 70 rows per month, only have empty cell ranges (the 12 months of the year are in column "M").

So I wanted to copy this data per month, to column "N", automatically without those intervals.

I'll try to explain it some other way:

coluna M          coluna N
M1=4                   N1=4
M2=                    N2=20
M3=20                  N3=2
M4=2                   N4=10
M5=                     
M6=
M7=
M8=10
    
asked by anonymous 26.01.2016 / 12:26

1 answer

5

Good morning @Mario Felicio

Follow the Macro code:

Sub CopyPaste()

    Dim ws As Worksheet
    Dim qtde As Integer
    Dim linFrom As Integer
    Dim linTo As Integer
    Dim colFrom As String
    Dim colTo As String

    'inicializando minhas variáveis
    colFrom = "M"
    colTo = "N"
    linFrom = 1
    linTo = 1

    'Definindo qual a planilha
    Set ws = Worksheets("Plan1")
    'Identificando qual a última linha
    qtde = ws.Cells(Rows.Count, colFrom).End(xlUp).Offset(1, 0).Row

    'fazendo um loop até o meu último registro da coluna de Origem
    While linFrom <= qtde
        'verificando se a minha célula de origem possui valor
        If ws.Cells(linFrom, colFrom) <> "" Then
            'se houver valor, copio para a minha célula de destino
            ws.Cells(linTo, colTo) = ws.Cells(linFrom, colFrom)
            'passando para minha próxima linnha de destino
            linTo = linTo + 1
        End If
        'passando para minha próxima linha de origem
        linFrom = linFrom + 1

    Wend

End Sub

Note that I put the columns M and N into variables to make it easier if you need to change the source and target columns.

Just copy the code above, enter the Developer / Visual Basic menu, paste this code and save. Then just click Macros and select it.

Remembering that your spreadsheet will have to be saved with Macros support.

I hope I have helped!

    
26.01.2016 / 14:06