Copy a specified range and paste to a sheet
下面的代码与特定元素分开运行良好:
1 2 | rowstopasteperiodsWks.Range(("A14"), ActiveCell.Offset(-pasteCount, 0)).Copy_ Destination:=Worksheets("Input").Range(LastRowPeriod,Offset(-pasteCount,0)) |
我正在尝试从RowsToPaste工作表中复制一定范围的单元格(A14和该单元格上方指定的(n)个单元格),并将此范围粘贴到"输入"工作表中,列D的最后一行(这样D列的最后一行将具有A14值,倒数第二个将具有A13值,依此类推。)
谢谢
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 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 |
如果您必须复制单元格" A14"并在其上方另外
1 2 | rowstopasteperiodsWks.Range("A14").Offset(-pasteCount).Resize(pasteCount + 1).Copy _ Destination:=Worksheets("Input").Cells(Rows.Count,"D").End(xlUp).Offset(1) |
如果必须从" A14"开始向上复制
1 2 | rowstopasteperiodsWks.Range("A14").Offset(-pasteCount+1).Resize(pasteCount).Copy _ Destination:=Worksheets("Input").Cells(Rows.Count,"D").End(xlUp).Offset(1) |
仅注意到一个明显的错误。
修复它,然后重试::
1 2 | rowstopasteperiodsWks.Range(("A14"), ActiveCell.Offset(pasteCount*-1, 0)).Copy_ Destination:=Worksheets("Input").Range(LastRowPeriod,Offset(pasteCount*-1,0)) |