繁体   English   中英

将日期复制/粘贴到另一个工作簿中WS上的列中

[英]Copy/Paste Date format into a column on a WS in another workbook

我正在使用来自两个不同来源的数据。 每个来源的日期/时间格式都不同。 我需要将两组数据放入一个工作表中并删除重复项。 日期格式的差异阻止了这种情况。 我试图将日期格式从工作簿“ A”复制到工作簿“ B”中现有数据的列范围,以便当我将数据从工作簿“ A”复制到工作簿“ B”的末尾时,日期格式将匹配。

工作簿“ A”中的日期格式为:

在此处输入图片说明

工作簿“ B”中的日期格式为:

在此处输入图片说明

我在下面提供了完整的代码。 但是这是我添加的行:

sourceWorksheet.Range("G2").Copy destinationWorksheet.Range("G2:H2000").PasteSpecial(xlPasteFormats, Operation:=xlNone, SkipBlanks:=False)

这给了我以下错误:

运行时错误'1004':无法获取Range类的PasteSpecial属性

这是整个代码集:

Sub QA_1603_March()
'
    Dim ANS As Long
    Dim LR As Long
    Dim uRng As Range
    Dim she As Worksheet

    ANS = MsgBox("Is the March 2016 Swivel Master File checked out of SharePoint and currently open on this desktop?", vbYesNo + vbQuestion + vbDefaultButton1, "Master File Open")
    If ANS = vbNo Or IsWBOpen("Swivel - Master - March 2016") = False Then
        MsgBox "The required workbook is not currently open. This procedure will now terminate.", vbOKOnly + vbExclamation, "Terminate Procedure"
        Exit Sub
    End If

    Call Verification_Format_WS

    Dim sourceWorkBook As Workbook
        Set sourceWorkBook = Workbooks("Verification Temp.xlsx")
    Dim destinationWorkbook As Workbook
        Set destinationWorkbook = Workbooks("Swivel - Master - March 2016.xlsm")
    Dim sourceWorksheet As Worksheet
        Set sourceWorksheet = sourceWorkBook.Sheets("Verification")
    Dim destinationWorksheet As Worksheet
        Set destinationWorksheet = destinationWorkbook.Sheets("Validation")

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    sourceWorksheet.Cells.EntireRow.Hidden = False

    sourceWorksheet.Range("G2").Copy destinationWorksheet.Range("G2:H2000").PasteSpecial(xlPasteFormats, Operation:=xlNone, SkipBlanks:=False)

    For LR = sourceWorksheet.Range("J" & Rows.Count).End(xlUp).row To 2 Step -1
        If sourceWorksheet.Range("J" & LR).Value <> "3" Then
            If uRng Is Nothing Then
                Set uRng = sourceWorksheet.Rows(LR)
            Else
                Set uRng = Union(uRng, sourceWorksheet.Rows(LR))
            End If
        End If
    Next LR

    If Not uRng Is Nothing Then uRng.Delete
        For Each she In destinationWorkbook.Worksheets
        If she.FilterMode Then she.ShowAllData
    Next

    With sourceWorksheet.Sort
        With .SortFields
            .Clear
            .Add Key:=Range("A2:A2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Add Key:=Range("G2:G2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Add Key:=Range("B2:B2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Add Key:=Range("C2:C2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Add Key:=Range("D2:D2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Add Key:=Range("E2:E2000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        End With
        .SetRange Range("A2:AE2000")
        .Apply
    End With

    sourceWorksheet.Cells.WrapText = False

    Dim lastRow As Long
    lastRow = sourceWorksheet.Range("A" & Rows.Count).End(xlUp).row
    Dim destinationRow As Long
    destinationRow = destinationWorksheet.Cells(Rows.Count, 1).End(xlUp).row + 1
    sourceWorksheet.Range("A2:J" & lastRow).Copy destinationWorksheet.Range("A" & destinationRow)

    Call Verification_Save
    Call Verification_Delete_Temp_Workbook

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic

End Sub

我从这里找到的示例中尝试了一些变体,但是我不断遇到一种或另一种错误。

问题是这一行:

sourceWorksheet.Range("G2").Copy destinationWorksheet.Range("G2:H2000").PasteSpecial(xlPasteFormats, Operation:=xlNone, SkipBlanks:=False)

是无效的语法。

Copy方法具有一个可选参数,以将Range对象作为目标传递。 通过将PasteSpecial方法添加到该范围,该方法不再有效。

尝试这个:

sourceWorksheet.Range("G2").Copy 
destinationWorksheet.Range("G2:H2000").PasteSpecial(xlPasteFormats, Operation:=xlNone, SkipBlanks:=False)

正如@Scott所说,复制行需要分为两行。 但是,您不需要Operation:=xlNone, SkipBlanks:=False部分,因为默认情况下会将它们设置为该部分。 以下应该工作。

sourceWorksheet.Range("G2").Copy
destinationWorksheet.Range("G2:H2000").PasteSpecial xlPasteFormats

*注意:在这种情况下,您不需要括号即可传递参数。

暂无
暂无

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

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