+ Reply to Thread
Results 1 to 7 of 7

Copy & paste a row (without leaving an empty row behind!) if a cell contains a value

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    41

    Copy & paste a row (without leaving an empty row behind!) if a cell contains a value

    Hi all!

    I have 2 worksheets, one called "Consolidated" and one called "Converted".

    I would like to have a spreadsheet where I press the update button and a macro runs that searches for the word "Converted" in column E on the Consolidated spreadsheet (the original). When it finds it, I would like the entire row to be cut and pasted onto the "Converted" worksheet.

    I have managed to do this with my current code:

      Sub All_Loops()
    Dim sheetName As Variant
    For Each sheetName In Array("Converted")
    
                      Sheets(sheetName).Range("A7:XFD1048576").Delete
    Next sheetName
    
    
    Dim erow As Long
    Dim w1 As Worksheet
    Dim w2 As Worksheet
    
         Set w1 = Sheets("Consolidation")
         Set w2 = Sheets("Converted")
    
              x = 7
            Do While Cells(x, 5) <> ""
    
       
    
            If Cells(x, 5) = "Converted" Then
    
            w1.Rows(x).Cut
    
            w2.Activate
    
            erow = w2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    
            ActiveSheet.Paste Destination:=w2.Rows(erow)
    
            End If
    
    
        Worksheets("Consolidation").Activate
         x = x + 1
         Loop
    
        Application.CutCopyMode = False
    
     
    
    End Sub
    However when this macro runs, the row left behind on the original sheet is a blank row and I don't want this. I would like it to be continuous spreadsheet, not dotted with random blank rows!

    If anybody can see a solution to this it would be greatly appreciated!

    Thanks!!

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

    Re: Copy & paste a row (without leaving an empty row behind!) if a cell contains a value

    You need to loop back ward if you wish to delete(cut) a row.

    Option Explicit
    Sub All_Loops()
    Dim sheetName As Variant
    For Each sheetName In Array("Converted")
    
    Sheets(sheetName).Range("A7:XFD1048576").Delete
    Next sheetName
    
    
    Dim i As Long
    Dim w1 As Worksheet
    Dim w2 As Worksheet
    
    Set w1 = Sheets("Consolidation")
    Set w2 = Sheets("Converted")
    
       
     With w1
    
           For i = .Cells(.Rows.Count, "E").End(xlUp).Row To 7 step -1
        
                If .Cells(i, 5) = "Converted" Then
                  .Rows(i).Cut
                   w2.Cells(Rows.Count, 1).End(xlUp).Offset(1).Paste
                End If
        
            Application.CutCopyMode = False
         
         Next
     End With
    End Sub

  3. #3
    Registered User
    Join Date
    11-15-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Copy & paste a row (without leaving an empty row behind!) if a cell contains a value

    Hi AB33,

    Thanks for replying! I tried the code but it seems to be breaking on this part:

    w2.Cells(Rows.Count, 1).End(xlUp).Offset(1).Paste
    Thanks!

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

    Re: Copy & paste a row (without leaving an empty row behind!) if a cell contains a value

    Sorry! My bad!

    Replace

    .Rows(i).Cut
                   w2.Cells(Rows.Count, 1).End(xlUp).Offset(1).Paste
    with
     .Rows(i).Cut w2.Cells(Rows.Count, 1).End(xlUp).Offset(1)
    One line

  5. #5
    Registered User
    Join Date
    11-15-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Copy & paste a row (without leaving an empty row behind!) if a cell contains a value

    Hey - did that! Unfortunately it is the same result as my code - the blank rows are still left behind on the original sheet

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

    Re: Copy & paste a row (without leaving an empty row behind!) if a cell contains a value

    Please attaché a sample

    Try these lines

     .Rows(i).copy w2.Cells(Rows.Count, 1).End(xlUp).Offset(1)
                  .Rows(i).Delete

  7. #7
    Registered User
    Join Date
    11-15-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Copy & paste a row (without leaving an empty row behind!) if a cell contains a value

    Awesome that line of code worked! Thanks so much!

+ 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. [SOLVED] Leaving a Cell Empty
    By Bob Folks in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-08-2014, 01:17 AM
  2. [SOLVED] Copy and paste from one sheet to another leaving out hidden cells
    By si_1970 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-20-2013, 06:41 AM
  3. copy and paste to next empty cell
    By Friday in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-15-2011, 01:00 AM
  4. Copy/Paste if cell below is empty
    By poptcorn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-30-2011, 09:36 AM
  5. [SOLVED] Leaving an empty cell empty
    By GRL in forum Excel General
    Replies: 5
    Last Post: 04-22-2006, 12:50 PM

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