VBA copy cells to another sheet

linkdemz

New Member
Joined
Sep 3, 2019
Messages
4
Hi,

I am wanting a code that will copy any cells within the Range of A10 to A30 that have any data in them, to another worksheet, but ignore any cells that are blank and not copy the blank cells over.

I have managed to get cells to copy when i push the macro button but what I want to happen which is currently not for example is... If I only fill the A10 to A15 I only want it to check the cells A10 to A30 recognize it stops at A15 and only copy A10 to A15.

I hope that makes sense.

Thanks for any help that can be given.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello and welcome.

Would help if you posted what code you have. You also don't state where you want the copied cell pasted to.

This code copies and pastes to next blank row in the 2nd sheet

Code:
Sub CopyCells()
    Dim c As Range
    Dim nr As Long 'next row
    Dim wsPaste As Worksheet, wsCopy As Worksheet
    
    'change sheet names as required
    Set wsCopy = Sheets("Sheet1")
    Set wsPaste = Sheets("Sheet2")
    
    nr = wsPaste.Range("A" & Rows.Count).End(xlUp).Row + 1
    
    For Each c In wsCopy.Range("A10:A30")
        If c <> "" Then 'only copy if not blank
            c.Copy wsPaste.Range("A" & nr)
            Application.CutCopyMode = False
            nr = nr + 1
        End If
    Next c


End Sub
 
Upvote 0
Hi & welcome to MrExcel.
How about
Code:
Sub linkdemz()
   Range("A10:A30").SpecialCells(xlConstants).Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
End Sub
This assumes the cells are hard values, rather than formulae
 
Last edited:
Upvote 0
Hello and welcome.

Would help if you posted what code you have. You also don't state where you want the copied cell pasted to.

This code copies and pastes to next blank row in the 2nd sheet

Code:
Sub CopyCells()
    Dim c As Range
    Dim nr As Long 'next row
    Dim wsPaste As Worksheet, wsCopy As Worksheet
    
    'change sheet names as required
    Set wsCopy = Sheets("Sheet1")
    Set wsPaste = Sheets("Sheet2")
    
    nr = wsPaste.Range("A" & Rows.Count).End(xlUp).Row + 1
    
    For Each c In wsCopy.Range("A10:A30")
        If c <> "" Then 'only copy if not blank
            c.Copy wsPaste.Range("A" & nr)
            Application.CutCopyMode = False
            nr = nr + 1
        End If
    Next c


End Sub

Hi,

Thankyou so much that worked amazing!.

Just to push the envelope a little further however, how would i modify that to copy from two different columns of sheet1 and paste to two different colums of sheet2.

For example if i wanted to only copy cells that contain data in row A + X of sheet1 and past them into rows C + E of sheet 2?

Thank you so much for your help!!!
 
Upvote 0
How about
Code:
Sub linkdemz()
   Range("A10:A30").SpecialCells(xlConstants).Copy Sheets("Sheet2").Range("C" & Rows.Count).End(xlUp).Offset(1)
   Range("X10:X30").SpecialCells(xlConstants).Copy Sheets("Sheet2").Range("E" & Rows.Count).End(xlUp).Offset(1)
End Sub
 
Upvote 0
How about
Code:
Sub linkdemz()
   Range("A10:A30").SpecialCells(xlConstants).Copy Sheets("Sheet2").Range("C" & Rows.Count).End(xlUp).Offset(1)
   Range("X10:X30").SpecialCells(xlConstants).Copy Sheets("Sheet2").Range("E" & Rows.Count).End(xlUp).Offset(1)
End Sub

Hi Fluff,

Thanks for the reply. I have tried the code you posted but i am getting an error message saying "Copy method of range class failed" for the second line.

Could you help resolve it please?
 
Upvote 0
Are the values in X10:X30 hard values or formulae?
Is there any sheet protection?
Do you have any merged cells?
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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