VBA Excel声明xlValues作为子参数

我正在尝试编写一个可重用的子将列复制到另一列。 在这种情况下,我想添加第三个variables来selectPasteSpecial的types。 现在它是xlValues并且只粘贴值。 我希望能使它更通用:价值,格式,配方…各种粘贴特价商品。

我想知道的是如何声明这是一个参数,并将Excel作为参数接受它。 有没有像字典,我可以查看这些东西的types?

Sub copyPasteColumn(column1 As String, column2 As String) 'just paste 1 column to another, values only Sheets("BILLING DATA").Columns(column1).Copy Columns(column2).PasteSpecial xlValues End Sub 

只需添加另一个参数XlPasteType :

 Sub CopyColumn(ByVal col1 As String, ByVal col2 As String, ByVal xOperType As XlPasteType) 'here your code End Sub 

用法:

 CopyColumn "A", "B", xlPasteComments 'or any other type of `XlPasteType` 

更多细节: Range.PasteSpecial方法(Excel)

这将以类似于Intellisense的方式显示PasteType,如果您不selectPasteType,则默认粘贴值。

编辑 – 这使用列号而不是字母 – 列A = 1等,但可以很容易地使用您的字母的方法。

 Enum PasteType xlPasteAll = -4104 'Everything will be pasted. xlPasteAllExceptBorders = 7 'Everything except borders will be pasted. xlPasteAllMergingConditionalFormats = 14 'Everything will be pasted and conditional formats will be merged. xlPasteAllUsingSourceTheme = 13 'Everything will be pasted using the source theme. xlPasteColumnWidths = 8 'Copied column width is pasted. xlPasteComments = -4144 'Comments are pasted. xlPasteFormats = -4122 'Copied source format is pasted. xlPasteFormulas = -4123 'Formulas are pasted. xlPasteFormulasAndNumberFormats = 11 'Formulas and Number formats are pasted. xlPasteValidation = 6 'Validations are pasted. xlPasteValues = -4163 'Values are pasted. xlPasteValuesAndNumberFormats = 12 End Enum Sub copyPasteColumn(column1 As Long, column2 As Long, Optional lType As PasteType = xlPasteValues) Sheets("BILLING DATA").Columns(column1).Copy Columns(column2).PasteSpecial lType End Sub Sub Test() copyPasteColumn 1, 2, xlPasteValues End Sub