Search code examples
vbaexcelcut

Code to Cut a range and paste it on a cell like if it was excel (VBA)


I'm trying to use this code to cut and paste some data:

For c = p To 12
    If Sheets("Hoja2").Cells(4, 3 + c + yy).Value <> "Total general" Then
        c2 = p - 1
        dif = Sheets("Hoja2").Cells(4, 3 + c + yy).Value - Sheets("Hoja2").Cells(4, 3 + c2 + yy).Value
        If Sheets("Hoja2").Cells(4, 3 + c + yy).Value - Sheets("Hoja2").Cells(4, 3 + c2 + yy).Value <> 1 Then
            Sheets("Tabla Final REV").Range(Cells(5, (4 * (e * 3 + t) + 4 * (1 - t) + p - 1)), Cells(variablefinalfila, 15)).Cut Sheets("Tabla Final REV").Range(Cells(5, (4 * (e * 3 + t) + 4 * (1 - t) + p + dif - 1 - 1)), Cells(5, (4 * (e * 3 + t) + 4 * (1 - t) + p + dif - 1 - 1)))
        End If
    Else
        Exit For
    End If
Next c

But it doesn't paste anything.

In other words my code is just:

For x = 1 to 12 
     if condition
      if othercondition
         Sheets("Mysheet").Range(cells(number,number2), cells(number3,number4)). Cut Sheets("Mysheet").Range(cells(anumber,anumber2),cells(anumber,anumber2))

What can I do to improve it??


Solution

  • Qualify those Cells may fix it:

     With Sheets("Tabla Final REV")
        .Range(.Cells(5, (4 * (e * 3 + t) + 4 * (1 - t) + p - 1)), .Cells(variablefinalfila, 15)).Cut .Range(.Cells(5, (4 * (e * 3 + t) + 4 * (1 - t) + p + dif - 1 - 1)), .Cells(5, (4 * (e * 3 + t) + 4 * (1 - t) + p + dif - 1 - 1)))
     End With
    

    Notice the dots in front of the .Cells