Copy excel sheet range from one sheet to another if not blank

I'm working as a newbie in excel VBA and can't figure this one out.
I have sheet "Main" with a range from A1 to E10000.  When I press CommandButton1 I would like the content of the range that are not blank to copy to sheet "Reconciliation" in range A1 to E10000 and then also clear values in the range of F1 to F10000.

Not sure if it matters but I have both text and number values in the range of A1 to E10000.  I have no formulas in any of the ranges.
Who is Participating?
Martin LissOlder than dirtCommented:
Here's a macro you can run. It assumes you want F1 to F10000 to be cleared on Main. If not then change line 19 from wsM to wsR.

Sub DoReconciliation()

Dim lngRow As Long
Dim lngCol As Long
Dim wsM As Worksheet
Dim wsR As Worksheet

Set wsM = Sheets("Main")
Set wsR = Sheets("Reconciliation")

For lngRow = 1 To 10000
    For lngCol = 1 To 5
        If wsM.Cells(lngRow, lngCol) <> "" Then
            wsR.Cells(lngRow, lngCol) = wsM.Cells(lngRow, lngCol)
        End If

End Sub

Open in new window

Martin LissOlder than dirtCommented:
Let's assume this was your complete range.
rangeIs this the result you want?
rmc71291Author Commented:
Oh sorry no. Just paste nonblank cells in same location on other sheet.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Martin LissOlder than dirtCommented:
So then I must assume that there's already some data in the Reconciliation sheet. True?
Martin LissOlder than dirtCommented:
And which sheet should have range F1 to F10000 cleared?
Rob HensonFinance AnalystCommented:
I believe you can just Copy the whole range but when pasting use the paste Special option and choose the Skip Blanks option. This means that if a blank cell has been copied, it will be ignored when pasting and as such will not overwrite existing values in the destination sheet with a blank cell.

Rob H
rmc71291Author Commented:
This was exactly what I needed!  I did change from wsM to wsR.  Most sincere thanks!
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.