+ Reply to Thread
Results 1 to 7 of 7

Copy and paste row if a cell has a value

Hybrid View

  1. #1
    Registered User
    Join Date
    08-19-2012
    Location
    Denver
    MS-Off Ver
    Excel 2007
    Posts
    85

    Copy and paste row if a cell has a value

    Hello,

    I am using the following code to copy an entire row when that row has data in column Y, and then paste that row into a different worksheet.

    Sub CopyAndPaste()
    
    Call CopyAndPasteSpecial
    
    Application.ScreenUpdating = False
    NR = 2
    Dim cel As Range
    With Sheets("Processor")
         For Each cel In .Range("Y6:Y" & .Cells(.Rows.Count, "Y").End(xlUp).Row).SpecialCells(2)
             cel.EntireRow.Copy
             Sheets("Results").Range("A" & NR).PasteSpecial Paste:=xlPasteValues
             NR = NR + 1
        Next cel
     End With
     Application.CutCopyMode = False
     Application.ScreenUpdating = True
     
    End Sub
    Because the data in column Y is generated via a formula, the first thing I'm doing is copying and pasting special as values.

    Sub CopyAndPasteSpecial()
    
        Sheets("Processor").Select
        Range("Y6:Y27").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
    End Sub
    Does anyone know why calling this second macro would cause the first macro to return every row (even those without data in column Y)?

    THANK YOU!

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy and paste row if a cell has a value

    Mattman,
    The first code pastes VALUES ONLY, so why do you to paste them again?
    If you are calling the first macro from the second macro, the first amro has to complete running until the sub ends, unless otherwise, you instructed to stop somewhere.

  3. #3
    Registered User
    Join Date
    08-19-2012
    Location
    Denver
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Copy and paste row if a cell has a value

    The macro doesn't seem to work if the cells in column Y are pulling data from other cells (gets an error); therefore, I ran the copy and paste special macro BEFORE running the one you wrote.

    But when I run the paste special macro and then the one you wrote, it copies and pastes all of the rows, not just ones with values in column Y.

  4. #4
    Forum Contributor
    Join Date
    08-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2007, MS 365 (Windows 10 Pro 64-bit)
    Posts
    818

    Re: Copy and paste row if a cell has a value

    Try this

    Sub CopyAndPaste()
    
    Call CopyAndPasteSpecial
    
    Application.ScreenUpdating = False
    NR = 2
    Dim cell As Range
    With Sheets("Processor")
    
         For Each cell In .Range("Y6:Y" & .Range("Y" & Rows.Count).End(xlUp).Row) '.SpecialCells(2)
         If cell.Value <> "" Then
             cell.EntireRow.Copy
             Sheets("Results").Range("A" & NR).PasteSpecial Paste:=xlPasteValues
             NR = NR + 1
             End If
        Next cell
     End With
     Application.CutCopyMode = False
     Application.ScreenUpdating = True
    
    End Sub
    
    Sub CopyAndPasteSpecial()
    
        Sheets("Processor").Select
        Range("Y6:Y27").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
    End Sub

  5. #5
    Forum Contributor
    Join Date
    08-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2007, MS 365 (Windows 10 Pro 64-bit)
    Posts
    818

    Re: Copy and paste row if a cell has a value

    Try this

    Sub CopyAndPaste()
    
    Call CopyAndPasteSpecial
    
    Application.ScreenUpdating = False
    NR = 2
    Dim cell As Range
    With Sheets("Processor")
    
         For Each cell In .Range("Y6:Y" & .Range("Y" & Rows.Count).End(xlUp).Row) '.SpecialCells(2)
         If cell.Value <> "" Then
             cell.EntireRow.Copy
             Sheets("Results").Range("A" & NR).PasteSpecial Paste:=xlPasteValues
             NR = NR + 1
             End If
        Next cell
     End With
     Application.CutCopyMode = False
     Application.ScreenUpdating = True
    
    End Sub
    
    Sub CopyAndPasteSpecial()
    
        Sheets("Processor").Select
        Range("Y6:Y27").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
    End Sub

  6. #6
    Forum Contributor
    Join Date
    08-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2007, MS 365 (Windows 10 Pro 64-bit)
    Posts
    818

    Re: Copy and paste row if a cell has a value

    Try This


    Sub CopyAndPaste()
    Application.ScreenUpdating = False
    
    Range("Y6:Y27") = Range("Y6:Y27").Value
    
    NR = 2
    Dim cell As Range
    With Sheets("Processor")
    
         For Each cell In .Range("Y6:Y" & .Range("Y" & Rows.Count).End(xlUp).Row)
         If cell.Value <> "" Then
             cell.EntireRow.Copy
             Sheets("Results").Range("A" & NR).PasteSpecial Paste:=xlPasteValues
             NR = NR + 1
             End If
        Next cell
     End With
     Application.CutCopyMode = False
     Application.ScreenUpdating = True
    
    End Sub
    Last edited by tuongtu3; 07-31-2013 at 09:13 PM.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy and paste row if a cell has a value

    The macro doesn't seem to work if the cells in column Y are pulling data from other cells (gets an error);

    Regardless of the value of column Y, my code converts the formulae in to values if that cell is empty.
    o you want to copy all cells(Including the blank), and convert them in to values?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 6
    Last Post: 07-27-2013, 09:02 AM
  2. Replies: 8
    Last Post: 07-08-2013, 06:03 AM
  3. [SOLVED] Copy cell A1 and paste value into B1 if B1 has content, paste to B2...etc
    By mhopke in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-09-2012, 09:39 PM
  4. How to select cell C1, copy paste then C2, copy paste then C3 etc
    By s45yth in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-07-2011, 07:15 AM
  5. [SOLVED] Copy and Paste macro needs to paste to a changing cell reference
    By loulou in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2005, 07:06 AM

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