【问题标题】:Copy a specified range and paste to a sheet复制指定范围并粘贴到工作表
【发布时间】:2017-01-13 12:25:20
【问题描述】:

除了特定元素之外,以下代码运行良好:

rowstopasteperiodsWks.Range(("A14"), ActiveCell.Offset(-pasteCount, 0)).Copy_
      Destination:=Worksheets("Input").Range(LastRowPeriod,Offset(-pasteCount,0))

我正在尝试从 RowsToPaste 表中复制一系列单元格(A14 和此单元格上方的指定 (n) 个单元格),并将此范围粘贴到输入表中的 D 列行中的最后一个单元格(这样 D 列中的最后一行将具有 A14 值,倒数第二行将具有 A13 值等)

谢谢

完整代码:

Sub UpdateLogWorksheet()

        Dim historyWks As Worksheet
        Dim inputWks As Worksheet

        Dim nextRow As Long
        Dim oCol As Long

        Dim myCopy As Range
        Dim myTest As Range

        Dim lRsp As Long

        Set inputWks = Worksheets("Input")
        Set historyWks = Worksheets("Data")
        Set rowstopasteperiodsWks = Worksheets("RowsToPaste")

        Dim lng As Long
        Dim pasteCount As Long
        pasteCount = Worksheets("RowsToPaste").Cells(2, 6)
        periodsCopy = Worksheets("RowsToPaste").Range("A12")

        LastRowPeriod = Cells(Rows.Count, 4).End(xlUp).Row
        oCol = 3 ' staff info is pasted on data sheet, starting in this column



      rowstopasteperiodsWks.Range(("A14"), ActiveCell.Offset(-pasteCount, 0)).Copy_
      Destination:=Worksheets("Input").Range(LastRowPeriod,Offset(-pasteCount,0))

        'check for duplicate staff number in database
        If inputWks.Range("CheckAssNo") = True Then
          lRsp = MsgBox("Order ID already in database. Update record?", vbQuestion + vbYesNo, "Duplicate ID")
          If lRsp = vbYes Then
            UpdateLogRecord
          Else
            MsgBox "Please change Order ID to a unique number."
          End If

        Else

          'cells to copy from Input sheet - some contain formulas
          Set myCopy = inputWks.Range("Entry")

          With historyWks
              nextRow = .Cells(.Rows.Count, "A").End(xlUp).Row
          End With

          With inputWks
              'mandatory fields are tested in hidden column
              Set myTest = myCopy.Offset(0, 2)

              If Application.Count(myTest) > 0 Then
                  MsgBox "Please fill in all the cells!"
                  Exit Sub
              End If
          End With

        With historyWks
            'enter date and time stamp in record
            For lng = 1 To pasteCount
                With .Cells(nextRow + lng, "A")
                    .Value = Now
                    .NumberFormat = "mm/dd/yyyy hh:mm:ss"
                End With
                'enter user name in column B
                .Cells(nextRow + lng, "B").Value = Application.UserName
                'copy the data and paste onto data sheet
                myCopy.Copy
                .Cells(nextRow + lng, oCol).PasteSpecial Paste:=xlPasteValues, Transpose:=True
            Next lng
            Application.CutCopyMode = False
        End With




          'clear input cells that contain constants
          ClearDataEntry
      End If

    End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    如果你必须复制单元格“A14”pasteCount上面的更多单元格:

    rowstopasteperiodsWks.Range("A14").Offset(-pasteCount).Resize(pasteCount + 1).Copy _
      Destination:=Worksheets("Input").Cells(Rows.Count, "D").End(xlUp).Offset(1)
    

    如果您必须从“A14”开始向上复制pasteCount 单元格:

    rowstopasteperiodsWks.Range("A14").Offset(-pasteCount+1).Resize(pasteCount).Copy _
      Destination:=Worksheets("Input").Cells(Rows.Count, "D").End(xlUp).Offset(1)
    

    【讨论】:

    • 嘿@user3598756,这几乎正是我需要的(第一个代码),但是第二部分(粘贴)代码需要一个修改。我想开始粘贴我复制的内容,将 pasteCount 粘贴到“Cells(Row.Count, "D")”上方。我需要添加什么代码?我想以某种方式偏移到 Cells 位?
    • 我想我已经做到了 :) 谢谢 rowstoppasteperiodsWks.Range("A14").Offset(-pasteCount + 1).Resize(pasteCount).Copy _ Destination:=Worksheets("AssociateData" ).Cells(Rows.Count, "D").End(xlUp).Offset(-pasteCount + 1) 非常感谢
    【解决方案2】:

    刚刚注意到一个明显的错误。 修复它并重试::

    rowstopasteperiodsWks.Range(("A14"), ActiveCell.Offset(pasteCount*-1, 0)).Copy_
          Destination:=Worksheets("Input").Range(LastRowPeriod,Offset(pasteCount*-1,0))
    

    【讨论】:

    • 嘿,谢谢@Vityata。 “目标”行以红色突出显示为语法错误。知道为什么吗?
    • 您好,我现在收到一个错误“未定义子或函数”,它突出显示了第一个“偏移”字。有任何想法吗?这是我的代码:pastebin.com/407xdQVj
    • 谢谢。现在它停在 periodCopy = rowstoppasteperiodsWks.Range("A14") - 在你的代码中它是 A12,但它是 A14 - 它说“运行时错误”12“类型不匹配:(。
    • Vityata 有什么想法吗?
    猜你喜欢
    • 2012-09-02
    • 2022-11-27
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多