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
 
it works but the one problem that I have is the user name is generating with a space inbetween the first and last name but the named range has a underscore. is there a way that you could factor this in to the code?

many thanks

jamie
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this: line added to replace any spaces with underscore:

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
    'replace any spaces with underscore:
    [COLOR=#ff0000]sRangeName = Replace(sRangeName, " ", "_")[/COLOR]
    
    
    '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
It works perfect thank you Gallen :)

I was just wondering if I could ask you about automating this or even if it is possible?

so the usersname would be in A1 and the cells To Copy would be in B1 & C1

and it would basically just run through the list copying them and pasting them into
named ranges on Sheet 2?

I am really sorry for asking but seeing what is possible just gives me more ideas.

many thanks

Jamie
 
Upvote 0
Hi,

is there any chance that this can be done with columns instead of cells ? so it would copy B1:C1 based on the Name in A1?
 
Upvote 0
Hi,

so it would be something like the below

COLUMNA B C
ABC456789
DEF123456
GHI7891011
JKL12131415
MNO16171819

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>


with the named ranges on sheet 2 would be the values in column A
 
Upvote 0
Sorry, snowed under at work. Your example is confusing as the formatting went astray.

Based on this:

so it would copy B1:C1 based on the Name in A1?

I asked what would the value like be in B1:C1?

I think based on our previous solution, you wish to loop through all names in column A starting at cell A1. Then provide the solution I originally provided for each cell that has a value in column A? Is this correct?
 
Upvote 0
Just tried it and I'm still lost. Not enough info.

so this code:
Code:
'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
    'replace any spaces with underscore:

    sRangeName = Replace(sRangeName, " ", "_")

Where we copy B21:C21 and place it in B7, where would we paste it now?
 
Upvote 0

Forum statistics

Threads
1,216,272
Messages
6,129,822
Members
449,538
Latest member
cookie2956

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