Copy 2 columns to last row of sheet removing blanks?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Hi,
Having trouble with this

I'm wanting to copy from sheet1:
Columns C + G (only rows with data)
To
Sheet2 (columns A+B) last row

So Sheet2 should never have any blanks between rows

Appreciate any help

Was thinking maybe an advanced filter then copying the filtered range to sheet 2 but maybe an easier way
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
What if one of the cells in columns C & G has data and the other one is blank?
 
Upvote 0
Code:
Option Explicit


Sub foo()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim lrC As Long, lrG As Long, lrA As Long, lrB As Long
    lrC = s1.Range("C" & Rows.Count).End(xlUp).Row
    lrG = s1.Range("G" & Rows.Count).End(xlUp).Row
    Dim i As Long
    Application.ScreenUpdating = False
    For i = 1 To lrC
        lrA = s2.Range("A" & Rows.Count).End(xlUp).Row
        If Not IsNull(s1.Range("C" & i)) Then
            s1.Range("C" & i).Copy s2.Range("A" & lrA + 1)
        End If
    Next i


    For i = 1 To lrG
        lrB = s2.Range("B" & Rows.Count).End(xlUp).Row
        If Not IsNull(s1.Range("G" & i)) Then
            s1.Range("G" & i).Copy s2.Range("B" & lrB + 1)
        End If
    Next i


    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    MsgBox "Complete"


End Sub
 
Upvote 0
Here's my attempt:

Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow  As Long
    Dim lngPasteRow As Long
    Dim lngMyRow    As Long
    
    Application.ScreenUpdating = False

    lngLastRow = Sheets("Sheet1").Range("C:C,G:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lngMyRow = 2 To lngLastRow 'Starts from Row 2. Change to suit if necessary.
        If Len(Sheets("Sheet1").Range("C" & lngMyRow)) > 0 Or Len(Sheets("Sheet1").Range("G" & lngMyRow)) > 0 Then
            If lngPasteRow = 0 Then
                On Error Resume Next 'In case there's data in Sheet2
                    lngPasteRow = Sheets("Sheet2").Range("A:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                    If lngPasteRow = 0 Then
                        lngPasteRow = 2 'Default output row if there's no data on Sheet2. Change to suit if necessary.
                    End If
                On Error GoTo 0
            Else
                lngPasteRow = Sheets("Sheet2").Range("A:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
            End If
            With Sheets("Sheet2")
                .Range("A" & lngPasteRow) = Sheets("Sheet1").Range("C" & lngMyRow)
                .Range("B" & lngPasteRow) = Sheets("Sheet1").Range("G" & lngMyRow)
            End With
        End If
    Next lngMyRow
    
    Application.ScreenUpdating = True

End Sub

HTH

Robert
 
Upvote 0
Another way (assumes both sheets data starts in row 2) :

Code:
Sub FT()
Dim lr&
With Sheets("Sheet1")
    lr = .Cells(Rows.Count, "C").End(xlUp).Row
    .Range("C2:C" & lr).Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp)(2)
    .Range("G2:G" & lr).Copy Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp)(2)
End With
On Error Resume Next
Sheets("Sheet2").[A:A].SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 
Last edited:
Upvote 0
Thanks for all the solutions, will try in morning.

Forgot to mention but column G has formulas all the way down to row 10000 which return "" until C is populated.

Unsure if this affects row count
 
Upvote 0
Thanks for all the solutions, will try in morning.

Forgot to mention but column G has formulas all the way down to row 10000 which return "" until C is populated.

Unsure if this affects row count

Should not affect the macro I posted provided that the blanks in column C are really blanks.
 
Upvote 0
This will affect row count and in the case of my code it will affect the results because you indicated not to move cells that are blank. Your cells in G are populated. My code will not work for you because I looked for cells that are empty/null. I will not amend since you have two other opportunities. If they don't work, then post back and we will amend.
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,601
Members
452,658
Latest member
GStorm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top