Copy & Paste Using Defined Name Ranges

bloodmilksky

Board Regular
Joined
Feb 3, 2016
Messages
202
Hi Guys,

I was just wondering if anyone knows how I can adjust the below code so it will


  • Copy Cells B21 & C21 On sheet 1
  • To A Range Defined in B7
  • The range is on sheet 2 and is the users name

Sub CopySheet1A1toSheet2NextCellInColumnA() Dim LastRowSheet2 As Long LastRowSheet2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row Sheets("Sheet2").Cells(LastRowSheet2 - [Sheet2!A1<>""], "A").Value = [Sheet1!A1]End Sub

please any help would be greatly appreciated

many thanks

jamie
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Breaking down your request in to steps (I've ignored the code you had in there originally):
Code:
Sub CopySheet1A1toSheet2NextCellInColumnA()

Dim LastRowSheet2 As Long
Dim sRangeName As String


    'Copy Cells B21 & C21 On sheet 1
    Sheets("Sheet1").Range("B21:C21").Copy
    'To A Range Defined in B7
    sRangeName = Sheets("Sheet1").Range("B7").Value
    'The range is on sheet 2 and is the users name
    Sheets("Sheet2").Range(sRangeName).PasteSpecial xlPasteAll
    
    
    LastRowSheet2 = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    Sheets("Sheet2").Cells(LastRowSheet2 - [Sheet2!A1<>""], "A").Value = [Sheet1!A1]


End Sub
 
Upvote 0
THANK You thats awesome.

I was just wondering is there a way that you could do it as with the original code so the next time you went to copy and paste into that range it would identify that there is a value already their and move down to the row below and paste it and so on.
 
Upvote 0
Sheet 1 B7 would be Barry Johnson

and then the range on sheet 2 would be Barry_Johnson which would be cells b2:c33
 
Upvote 0
Ah. So you want the next available row within that range or the next available row below that range?

Sorry if I sound stupid, it's difficult when you don't see the actual workbook.:)
 
Upvote 0
Not sure what this line is trying to achieve:
Code:
Sheets("Sheet2").Cells(LastRowSheet2 - [Sheet2!A1<>""], "A").Value = [Sheet1!A1]

However this is tested and working based on your info:

Code:
Sub CopySheet1A1toSheet2NextCellInColumnA()

Dim lRow As Long
Dim sRangeName As String

    'Copy Cells B21 & C21 On sheet 1
    Sheets("Sheet1").Range("B21:C21").Copy
    'To A Range Defined in B7
    sRangeName = Sheets("Sheet1").Range("B7").Value
    'The range is on sheet 2 and is the users name


    'get next empty row in named range
    lRow = 1
    Do Until Sheets("Sheet2").Range(sRangeName).Cells(lRow, 1) = ""
        lRow = lRow + 1
    Loop
    'Paste Data
    Sheets("Sheet2").Range(sRangeName).Cells(lRow, 1).PasteSpecial xlPasteAll

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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