Copy lines and paste based on a value in an excel cell

Asked

Viewed 446 times

0

I would like to know how to copy a set of lines. For example, line 1 through 5 and paste on lines 10 through 15. This action would be done with the value 1 in another sheet and in cell A1.

I have a button with VBA to do this, but I wanted to do it automatically when the cell value is "28" or "29".

These are the codes I have on button. One adds and the other removes the lines.

Private Sub CommandButton1_Click()

Sheets("Fevereiro").Rows("5:9").Copy (Sheets("Fevereiro").Rows("11:15"))

End Sub

Private Sub CommandButton2_Click()

Worksheets("Fevereiro").Rows("11:15").Delete

End Sub

This is to try to do the following: The month of February is 28 days or 29. I have a formula FIMMÊS to give me the exact day if the month ends 28 or 29 depending on the year.

If from one year to the next the month has one more day, the VBA adds a certain number of lines in the sheet "February" (just below the last row filled) or removes if the following year only has 28 again.

1 answer

1

The simplest way to check the amount of days in a month is the following:

Ano = 2018
Mes = 2
DiasNoMes = Day(DateSerial(Ano, Mes + 1, 0))
MsgBox DiasNoMes

Which returns number 28, that is, the month of February 2018 has 28 days.

So a conditional logic if can be created with:

If DiasNoMes = 28 Then
    Sheets("Fevereiro").Rows("5:9").Copy (Sheets("Fevereiro").Rows("11:15"))
ElseIf DiasNoMes = 29 Then
    Worksheets("Fevereiro").Rows("11:15").Delete
End If
  • Thanks for the answer, but I’m not able to put this example into practice. I don’t know much about vba. The first code I put where? The If code is to enter something?

Browser other questions tagged

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