+ Reply to Thread
Results 1 to 4 of 4

Macro code error: code is near complete

Hybrid View

  1. #1
    Registered User
    Join Date
    12-06-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    6

    Macro code error: code is near complete

    Hi, I have written a macro code as below, however, there seems to be an error.

    I am trying to select all rows in sheet 1 with an X in column E to have their respective columns F, G, H & I to be copied and pasted into sheet 2 starting with cell A1, but sheet 2 should not contain any blank rows.

    Can anyone pls help to fine tune the macro?

    Many thanks.


    
    Sub Copy_Rows()
      
    'Copy rows containing X
    
    Sheets("Sheet1").Select
    For Each r In Sheets("Sheet1").UsedRange.Rows
        n = r.Row
            If Sheets("Sheet1").Cells(n, 5) = "X" Then
            Sheets("Sheet1").Range(Cells(n, 6), Cells(n, 9)).Copy
            Sheets("Sheet2").Select
            Range("A1").Select
            Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
                Else
        End If
        Next r
    
    End Sub
    Last edited by kashshaikh; 12-11-2011 at 10:02 PM.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Macro code error: code is near complete

    Forum rules require VBA code to be wrapped in code tags.

    Please edit your post to add the tags.

    [code]
    your code here
    [/code]

    Why not apply an auto filter to the column and copy all of the visible rows in one go?
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Registered User
    Join Date
    12-06-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro code error: code is near complete

    Hi, I have written a macro code as below, however, there seems to be an error.

    I am trying to select all rows in sheet 1 with an X in column E to have their respective columns F, G, H & I to be copied and pasted into sheet 2 starting with cell A1, but sheet 2 should not contain any blank rows.

    Can anyone pls help to fine tune the macro?

    Many thanks.

    
    
    Sub Copy_Rows()
    
    'Copy rows containing X
    
    Sheets("Sheet1").Select
    For Each r In Sheets("Sheet1").UsedRange.Rows
    n = r.Row
    If Sheets("Sheet1").Cells(n, 5) = "X" Then
    Sheets("Sheet1").Range(Cells(n, 6), Cells(n, 9)).Copy
    Sheets("Sheet2").Select
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Else
    End If
    Next r
    
    End Sub

  4. #4
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Macro code error: code is near complete

    Basic code - using autofilter, which is more efficient than looping.

    Try it on a back up copy first.

    Option Explicit
    
    Sub Copy_X_Cells()
    
        Dim lastrow As Long: lastrow = Cells(Rows.Count, "E").End(xlUp).Row
        
        Application.ScreenUpdating = False
        
        With Sheet1
            .AutoFilterMode = False
            .Range("E1:E" & lastrow).AutoFilter field:=1, Criteria1:="x"
            .Range("F1:I" & lastrow).SpecialCells(xlCellTypeVisible).Copy Sheet2.Range("A1")
            .AutoFilterMode = False
        End With
        
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    
    End Sub
    Sheet references are to sheet code names. Adjust them as required.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1