VBA, copy & past specific columns to prevent overwriting of existing data

excelquestion8

New Member
Joined
Feb 9, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi, I would like to only copy and paste columns A - L into another tab instead of the whole row. I would like to prevent overwriting data in columns after L. Here is my code:

VBA Code:
Sub CopyName()
Dim Check As Range, r As Long, lastrow2 As Long, lastrow As Long, lastrow3 As Long, lastrow4 As Long
Application.ScreenUpdating = False
lastrow = Worksheets("Assignments").UsedRange.Rows.Count
lastrow2 = Worksheets("name1").UsedRange.Rows.Count
lastrow3 = Worksheets("name2").UsedRange.Rows.Count
lastrow4 = Worksheets("name3").UsedRange.Rows.Count

If lastrow2 = 1 Then lastrow2 = 0
    For r = lastrow To 2 Step -1
        If Range("E" & r).Value = "name1" Then
            Rows(r).Copy Destination:=Worksheets("name1").Range("A" & lastrow2 + 2)
            lastrow2 = lastrow2 + 1
            Else:
        End If
    Next r
If lastrow3 = 1 Then lastrow3 = 0
    For r = lastrow To 2 Step -1
         If Range("E" & r).Value = "name2" Then
            Rows(r).Copy Destination:=Worksheets("name2").Range("A" & lastrow3 + 2)
            lastrow3 = lastrow3 + 1
            Else:
        End If
    Next r
If lastrow4 = 1 Then lastrow4 = 0
    For r = lastrow To 2 Step -1
         If Range("E" & r).Value = "name3" Then
            Rows(r).Copy Destination:=Worksheets("name3").Range("A" & lastrow4 + 2)
            lastrow4 = lastrow4 + 1
            Else:
        End If
    Next r

Application.ScreenUpdating = True
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi & welcome to MrExcel.
You can do that like
VBA Code:
Range("A"& r).Resize(, 12).Copy Destination:=Worksheets("name1").Range("A" & lastrow2 + 2)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,520
Messages
6,114,101
Members
448,548
Latest member
harryls

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