简体   繁体   English

在同一张纸上复制和粘贴

[英]Copy and Paste on the Same Sheet

I have a workbook that serves as a database with an Input page.我有一个工作簿,用作带有输入页面的数据库。 I want to make Column A dynamic, which will update header rows on all pages of the worksheet.我想使 A 列动态化,这将更新工作表所有页面上的 header 行。 I have created a macro that copies these header names from Column A on the Input Sheet, and pastes these values as headers on the next sheet.我创建了一个宏,从输入表的 A 列复制这些 header 名称,并将这些值作为标题粘贴到下一张表上。 Once these header rows are labeled they are copied on Sheet 2 a second time so they can be pasted as additional header rows to the right of the previously pasted values.一旦这些 header 行被标记,它们将被第二次复制到工作表 2 上,因此它们可以作为额外的 header 行粘贴到先前粘贴的值的右侧。 The reason is because they are values monitored at Start and Stop times, which will have different data stored at each time.原因是因为它们是在 Start 和 Stop 时间监控的值,每次都会存储不同的数据。 Also, I would like these header rows to have medium weight borders around them.另外,我希望这些 header 行周围有中等粗细的边框。 I have drafted the following code, but it only works partially correct by copying the first set as expected, however the second copy part does not work as well.我已经起草了以下代码,但它只能通过按预期复制第一组来部分正确,但是第二部分不能正常工作。 I was hoping to create a template sheet in the document, which would have Date, Start Time, Space, End time.我希望在文档中创建一个模板表,其中包含日期、开始时间、空间、结束时间。 This would mean the copy rows would need to be inserted after Start Time and again after End time in a dynamic manner so this list could grow.这意味着需要以动态方式在开始时间之后插入复制行,并在结束时间之后再次插入复制行,以便该列表可以增长。 Please see my attached code and thank you so much for any help.请查看我所附的代码,非常感谢您的帮助。

Sub CopyData2()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim wb As Workbook
Dim lRow As Long
Dim lCol1 As Long
Dim lCol2 As Long
Dim cRange As Range
Dim iCell As Range
Dim iRange As Range

Set wb = ThisWorkbook

Set ws1 = wb.Sheets(1)

Set ws2 = wb.Sheets(2)

lRow = ws1.Cells(Rows.Count, 1).End(xlUp).Row

lCol1 = ws2.Cells(12, Columns.Count).End(xlToLeft).Column

lCol2 = ws2.Cells(3, Columns.Count).End(xlToLeft).Column

ws1.Range("A13:A" & lRow).Copy

ws2.Range("C3").PasteSpecial xlPasteValues, Transpose:=True

Set cRange = ws2.Range(("C3"), ws2.Range("C3").End(xlToRight))

cRange.Select

cRange.Copy

ws2.Cells(3, lCol2).PasteSpecial xlPasteValues

End Sub

I don't understand what you're trying to do with the Start/Stop times but the try removing cRange.Select to fix the 2nd Copy/Paste我不明白你想用开始/停止时间做什么,但尝试删除cRange.Select来修复第二次复制/粘贴

I'd suggest replacing the last 4 lines with those below我建议用下面的替换最后 4 行

With ws2.Range(("C3"), ws2.Range("C3").End(xlToRight))
    ws2.Cells(3, lCol2).Resize(.Rows.Count,.Columns.Count).Value2 = .Value2
End With

in order to avoid both the Select, and redundant use of the clipboard.为了避免Select剪贴板的冗余使用。

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

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