Copy data till the last row from one sheet and paste in the last column available of the another sheet.

Harshil Mehta

Board Regular
Joined
May 14, 2020
Messages
85
Office Version
  1. 2013
Platform
  1. Windows
Expectation: I want to copy data from sheet7.Range G10 till the last row and paste in sheet4, Row No.10, last column available.

Problem: The below code does not copy or paste (no error is shown by the debugger). I dont understand where I am going wrong.

Could anyone please help me?

VBA Code:
Sub cf()

Dim lr, lcol1 As Long

With Sheet7


lr = .Cells(.Rows.Count, 7).End(xlUp).Row
lcol1 = Sheet4.Cells(10, Columns.Count).End(xlToLeft).Column + 1

.Range("G10" & lr).Copy

Sheet4.Cells(10, lcol1).PasteSpecial



End With
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You are missing part of the range to copy, you have G10 & lr, if lr was 20 then G1020 would be copied.
VBA Code:
Sub cf()
    Dim sh As Worksheet, ws As Worksheet
    Dim lr As Long, lcol1 As Long

    Set sh = Sheet7
    Set ws = Sheet4

    With sh
        lr = .Cells(.Rows.Count, 7).End(xlUp).Row
        lcol1 = ws.Cells(10, ws.Columns.Count).End(xlToLeft).Column + 1
        .Range("G10:G" & lr).Copy
        ws.Cells(10, lcol1).PasteSpecial
    End With

End Sub
 
Upvote 0
Hi there,

Try this:

VBA Code:
Option Explicit
Sub cf()

    Dim lr As Long, lcol1 As Long
    
    With Sheet7
        lr = .Cells(.Rows.Count, 7).End(xlUp).Row 'Finds the last row in Col. G of sheet code name 'Sheet7'
        lcol1 = Sheet4.Cells(10, Columns.Count).End(xlToLeft).Column + 1 'Finds the last column across row 10 in sheet code name 'Sheet4' and increments by one
        .Range("G10:G" & lr).Copy 'assumes last row is equal to or more than 10
        Sheet4.Cells(10, lcol1).PasteSpecial
        Application.CutCopyMode = False
    End With

End Sub

Regards,

Robert
 
Upvote 0
You are missing part of the range to copy, you have G10 & lr, if lr was 20 then G1020 would be copied.
VBA Code:
Sub cf()
    Dim sh As Worksheet, ws As Worksheet
    Dim lr As Long, lcol1 As Long

    Set sh = Sheet7
    Set ws = Sheet4

    With sh
        lr = .Cells(.Rows.Count, 7).End(xlUp).Row
        lcol1 = ws.Cells(10, ws.Columns.Count).End(xlToLeft).Column + 1
        .Range("G10:G" & lr).Copy
        ws.Cells(10, lcol1).PasteSpecial
    End With

End Sub
Thank you so much for your help.
 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,826
Members
449,051
Latest member
excelquestion515

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