VBA Excel将xlValues声明为sub中的参数

时间:2015-06-26 15:22:37

标签: excel vba excel-vba

我正在尝试编写一个可重用的子列来将列复制到另一列。在这种情况下,我想添加第三个变量来选择PasteSpecial的类型。现在它是xlValues并且只粘贴值。我希望能让它变得更加多样化:价值观,格式,配方......各种特殊的粘贴。

我想知道的是如何将其声明为参数,并且excel将其作为参数接受。 有没有字典我可以查看这些东西的类型?

   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

2 个答案:

答案 0 :(得分:1)

只需添加其他参数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 Method (Excel)

答案 1 :(得分:0)

这将以Intellisense的方式显示PasteType,如果您不选择PasteType,则默认情况下粘贴值。

编辑 - 这使用列号而不是字母 - 列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