VBA Offset excludes columns problem

drluke

Active Member
Joined
Apr 17, 2014
Messages
314
Office Version
  1. 365
Platform
  1. Windows
I want to copy columns A - I in a worksheet1 to another worksheet2 also in columns A-I. In my copy from worksheet1 I am finding the last row with referencing column B. The reason for doing that is because column A contains data from rows 2-5000, whereas columns B-I contains data in rows 2 - 400. I only want to copy rows 2-400 including column A (copy A2:I400). I have used "offset" in my code to include column A in the range I want to copy. Whilst column A is being copied, column I is not. I would appreciate any advice to over come this problem.
VBA Code:
Private Sub cmdAddProcessYes_Click()

    Dim lrow As Long
    Dim lastR As Long
    
    ThisWorkbook.Activate
        lrow = Worksheets("Additions").Cells(Worksheets("Additions").Rows.Count, "B").End(xlUp).Row
        
        lastR = Worksheets("Test").Cells(Worksheets("Test").Rows.Count, "A").End(xlUp).Row
        lastR = lastR + 1
            
        Worksheets("Additions").Range("B2:I" & lrow).Offset(, -1).Copy
            
        Worksheets("Test").Range("A" & lastR).PasteSpecial
            
        Worksheets("Test").Activate

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this:
VBA Code:
Private Sub cmdAddProcessYes_Click()

    Dim lrow As Long, lastR As Long
         lrow = Worksheets("Additions").Cells(Worksheets("Additions").Rows.Count, "B").End(xlUp).Row
        lastR = Worksheets("Test").Cells(Worksheets("Test").Rows.Count, "A").End(xlUp).Row + 1
        Worksheets("Additions").Range("A2:I" & lrow).Copy Worksheets("Test").Range("A" & lastR)
        Worksheets("Test").Activate

End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Private Sub cmdAddProcessYes_Click()

    Dim lrow As Long, lastR As Long
         lrow = Worksheets("Additions").Cells(Worksheets("Additions").Rows.Count, "B").End(xlUp).Row
        lastR = Worksheets("Test").Cells(Worksheets("Test").Rows.Count, "A").End(xlUp).Row + 1
        Worksheets("Additions").Range("A2:I" & lrow).Copy Worksheets("Test").Range("A" & lastR)
        Worksheets("Test").Activate

End Sub
Thank you so much - exactly what I wanted.
 
Upvote 0
maabadi's code does what you want, but you could also use your existing code with this one minor change

Your code
Rich (BB code):
Worksheets("Additions").Range("B2:I" & lrow).Offset(, -1).Copy

One letter changed
Rich (BB code):
Worksheets("Additions").Range("B2:J" & lrow).Offset(, -1).Copy
 
Upvote 0
maabadi's code does what you want, but you could also use your existing code with this one minor change

Your code
Rich (BB code):
Worksheets("Additions").Range("B2:I" & lrow).Offset(, -1).Copy

One letter changed
Rich (BB code):
Worksheets("Additions").Range("B2:J" & lrow).Offset(, -1).Copy
Thank you. Very helpful.
 
Upvote 0
You're welcome. Glad to contribute. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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