macro to copy and paste

Asked

Viewed 1,654 times

5

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

So I wanted to copy this data by month into the "N" column, automatically without these ranges.

I’ll try to explain it another 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

1 answer

5


Good morning @Mario Felicio

Follow the code of the Macro:

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 have put the columns M and N in variables to make it easier if you need to change the source and destination columns.

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

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

I hope I’ve helped!

  • friend Thomas. This is what I wanted, I changed "lymphrom=1";"Linto=1", to separate the months, I also changed "while lymphrom<=Qtde", to the last line of each month. ie I made 12 modules, each with these lines changed. is working.

  • Wonder...!!!

Browser other questions tagged

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