I'm building a macro to split a long table into multiple smalls tables, and I'd like to have the header saved in a variable to be able to quickly paste it on top of the smalls table.
I made a macro to do the first one (quite simple)
Sheets("Feuil1").Select
Rows("2:2").Select
Selection.Copy
Sheets("PageImpression").Select
Rows("2:2").Select
ActiveSheet.Paste
How can I keep my Selection.Copy
in a variable and how do I paste it when I need to?
The answer is to store the row into a variable. Then call that variable when you need it. Also, no need for copy / paste and select and all that :)
Option Explicit
Dim myHeader as Variant
myHeader = Sheets("Feuil1").Rows(2)
Sheets("PageImpression").Rows(2) = myHeader
Update
To use with a Range
Object, do the following:
Dim myHeader As Range
Set myHeader = Sheets(1).Rows(2)
Sheets(2).Rows(2).Value = myHeader.Value
Update 2
The even cleaner (memory + time saving) way to do this would be:
Option Explicit
Dim myHeader as Variant (or Range)
With Sheets("Feuil1")
myHeader = .Range(.Range("A1"),.Range("A" & .Columns.Count).End(xltoLeft).Column))
'for variable type Range, add "Set =" in front of line above
End With
Sheets("PageImpression").Range("A2") = myHeader
'For Range Sheets("PageImpression").Range("A2").Value = myHeader.Value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.