+ Reply to Thread
Results 1 to 6 of 6

Thread: Cut and paste rows: Code error

  1. #1
    Registered User
    Join Date
    08-23-2010
    Location
    Lisbon
    MS-Off Ver
    Excel 2003
    Posts
    8

    Cut and paste rows: Code error

    Hi!

    I'm working on a code to cut rows which have in column 4 the name "BS", to the end of my data (3 rows below). I wrote this code, but it has an error on the highlighted line "Apllication defined or object defined error".

    Can you please help me on this one?

    Thank you

    Ines


    Sub Retirar_BS()

    'Define lastrow

    Range("A3").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(3, 0).Select
    LastRow = ActiveCell.Row

    'Define variables

    reference = "BS"
    i = 4

    'Cut and paste to the end of the table

    While i <= Cells(Rows.Count, 4).End(xlUp).Row
    If Cells(i, 4) = reference Then
    Cells(i, 4).EntireRow.Cut Destination:=Cells(A & LastRow)
    LastRow = LastRow + 1
    Else
    i = i + 1
    End If
    Wend

    End Sub
    Last edited by nespena; 09-10-2010 at 10:10 AM.

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Cut and paste rows: Code error

    It would be like this:

    Cells(i, 4).EntireRow.Cut Destination:=Cells(lngLastRow, "A")

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    08-23-2010
    Location
    Lisbon
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Cut and paste rows: Code error

    Still doesn't work... the same error on the same line

    Could it be because when I cut the line the addresses referenced to in the code move? Should I just copy them to the end, and then delete the original ones?

  4. #4
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Cut and paste rows: Code error

    Maybe:

    Sub Retirar_BS()
    
    Dim lngLastRow As Long, lngWriteRow As Long
    Dim lngLoopRow As Long
    
    'Define variables
    
    lngLastRow = Cells(Rows.Count, 1).End(xlUp).Row
    lngWriteRow = lngLastRow + 3
    
    'Cut and paste to the end of the table
    
    For lngLoopRow = 4 To lngLastRow
    
        If Cells(lngLoopRow, 4) = "BS" Then
        
            Rows(lngLoopRow).Cut Destination:=Cells(lngWriteRow, "A")
            lngWriteRow = lngWriteRow + 1
            
        End If
        
    Next lngLoopRow
    
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Registered User
    Join Date
    08-23-2010
    Location
    Lisbon
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Cut and paste rows: Code error

    Thank you so much, it worked!

    Now I'll just a piece of code that will delete all the blank rows.

  6. #6
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Cut and paste rows: Code error

    Sub Retirar_BS()
    
    Dim lngLastRow As Long, lngWriteRow As Long
    Dim lngLoopRow As Long
    
    'Define variables
    
    lngLastRow = Cells(Rows.Count, 1).End(xlUp).Row
    lngWriteRow = lngLastRow + 3
    
    'Cut and paste to the end of the table
    
    For lngLoopRow = 4 To lngLastRow
    
        If Cells(lngLoopRow, 4) = "BS" Then
        
            Rows(lngLoopRow).Cut Destination:=Cells(lngWriteRow, "A")
            lngWriteRow = lngWriteRow + 1
            
        End If
        
    Next lngLoopRow
    
    For lngLoopRow = lngLastRow To 4 Step -1
    
        If Cells(lngLoopRow, 4) = "" Then
        
            Rows(lngLoopRow).Delete
            
        End If
        
    Next lngLoopRow
    
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0