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

tttommy2

Board Regular
Joined
Oct 1, 2012
Messages
60
Office Version
  1. 365
Platform
  1. Windows
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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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.

[TABLE="width: 894"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD] Expiry[/TD]
[TD]Symbol[/TD]
[TD]Expiry[/TD]
[TD]Symbol[/TD]
[TD]Min Expiry[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]42048[/TD]
[TD]aa[/TD]
[TD]=B3[/TD]
[TD]=C3[/TD]
[TD]{=MIN(IF(E3=INDEX(aData,,2),INDEX(aData,,1)))}[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]42077[/TD]
[TD]aa[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]42109[/TD]
[TD]bb[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]42051[/TD]
[TD]bb[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

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,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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