简体   繁体   English

有什么方法可以将行保存在excel宏中以备将来使用?

[英]Any way to save a row in an excel macro for future usage?

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? 如何将Selection.Copy保留在变量中,并在需要时将其粘贴?

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: 要与Range对象一起使用,请执行以下操作:

Dim myHeader As Range

Set myHeader = Sheets(1).Rows(2)

Sheets(2).Rows(2).Value = myHeader.Value

Update 2 更新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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM