VBA to copy first row in named range to whole named range.

tttommy2

Board Regular
Joined
Oct 1, 2012
Messages
55
Hello

I am looking for VBA to copy the first row in a named range and paste it to whole of the named range.

I am sure its very easy but I cant find anything.

Thank you

tttommy2
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hello

I am looking for VBA to copy the first row in a named range and paste it to whole of the named range.

I am sure its very easy but I cant find anything.

Thank you

tttommy2

Welcome to the board.
Try this after you replace myRng with the name of your range.
Rich (BB code):
Sub PasteToWholeNamedRange()
Dim R As Range
Set R = Range("myRng")  ' <-- put the name of your range between the quote marks
R.Rows(1).Copy R
End Sub
 
Upvote 0
Thank you Joe - that was quick.

I should have been more precise. I would like to copy paste formulae from first row of named range to whole of the named range. I don't want to copy the formats.

tttommy2
 
Upvote 0
Thank you Joe - that was quick.

I should have been more precise. I would like to copy paste formulae from first row of named range to whole of the named range. I don't want to copy the formats.

tttommy2
This pastes formulas only - retaining the initial format of each row in the named range.
Code:
Sub PasteToWholeNamedRange()
Dim R As Range
Set R = Range("myRng")  ' <-- put the name of your range between the quote marks
R.Rows(1).Copy
R.PasteSpecial xlPasteFormulas
Application.CutCopyMode = False
End Sub
 
Upvote 0
Sub PasteToWholeNamedRange()
Dim R As Range
Set R = Range("myRng") ' <-- put the name of your range between the quote marks
R.Rows(1).Copy
R.PasteSpecial xlPasteFormulas
Application.CutCopyMode = False
End Sub

Hi Joe

Thank you for your reply.

I am having a problem with your last VBA code above. The first row of my named range contains a single cell array formula and your VBA code does not seem to work with it. When I take out the array formula your VBA code works a treat.

My array formula is single cell formula and is not part of an array. When I do an ordinary copy paste, it copies fine.

Any ideas?

Thank you for your help.

tttommy2
 
Upvote 0
Sub PasteToWholeNamedRange()
Dim R As Range
Set R = Range("myRng") ' <-- put the name of your range between the quote marks
R.Rows(1).Copy
R.PasteSpecial xlPasteFormulas
Application.CutCopyMode = False
End Sub

Hi Joe

Thank you for your reply.

I am having a problem with your last VBA code above. The first row of my named range contains a single cell array formula and your VBA code does not seem to work with it. When I take out the array formula your VBA code works a treat.

My array formula is single cell formula and is not part of an array. When I do an ordinary copy paste, it copies fine.

Any ideas?

Thank you for your help.

tttommy2
What exactly "does not seem to work" - do you get an error or ....? Can you tell me what the layout of your named range is? And can you post the single-cell array formula?
 
Upvote 0
Its a very large and complex sheet so I have simplified it down to better explain the issue. I attach a jpg of the sheet showing formulas only.
copy%20paste%20formulas%20first%20row.jpg
 
Upvote 0
Its a very large and complex sheet so I have simplified it down to better explain the issue.

ABCDE
1 ExpirySymbolExpirySymbolMin Expiry
242048aa=B3=C3{=MIN(IF(E3=INDEX(aData,,2),INDEX(aData,,1)))}
342077aa
442109bb
542051bb

<tbody>
</tbody>

Range name = "aData" and refers to $C$2:$E$5

The array formula is like a MinIf formula. It shows the min expiry for each symbol. So for first 2 aa enties min expiry is 42048 and for 2 bb entries its 42051.

When I run your code I get Microsoft Visual Basic "Runtime error '1004'. You cannoot change part of an array."

However when I do simple copy paste of C2:E2 to C2:E5 it copies fine and everything is correct.There are 4 seperate array formulas in E2:E5 and they are all correct.

I hope I explain myself well

Thank you

tttommy2

I am using Excel 2010.


Sub PasteToWholeNamedRange()
Dim R As Range
Set R = Range("aData")
R.Rows(1).Copy
R.PasteSpecial xlPasteFormulas
Application.CutCopyMode = False
End Sub
 
Last edited:
Upvote 0
Not sure why you get that error, but maybe easier to try another approach. If I understand correctly, the first code I posted was OK except it transferred the formatting of the first row and you didn't want that.

Assuming I understand correctly, see if this works for you:
Code:
Sub PasteToWholeNamedRange()
Dim R As Range
Set R = Range("aData")  
R.Rows(1).Copy R
'Add some code to format rows of aData after the first as desired
'Maybe
R.Rows("2:" & R.Rows.Count).ClearFormats
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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