[英]Excel VBA copy range from one sheet and paste it one cell at a time in another sheet
I'm trying to copy a range of data from one sheet and paste it, one cell at a time, in another sheet.我正在尝试从一张纸上复制一系列数据并将其粘贴到另一张纸上,一次一个单元格。
Let's say Sheet1 has the data in range A1 to C5.假设 Sheet1 的数据范围为 A1 到 C5。 A1 till last row needs to be copied and pasted in Sheet2 but one cell at a time starting from first not empty cell in column A till last copied data.
A1 到最后一行需要复制并粘贴到 Sheet2 中,但一次一个单元格从 A 列中的第一个非空单元格开始直到最后复制的数据。 Is this possible?
这可能吗?
The reason why this has to be done one cell at a time is because for every updated cell a small.txt file with the new value for this cell will be created.必须一次一个单元格执行此操作的原因是,对于每个更新的单元格,都会创建一个包含该单元格新值的 small.txt 文件。 This file is then used to sync data across multiple excel files.
然后使用此文件跨多个 excel 文件同步数据。
Thanks in advance提前致谢
I wouldn't use the copy/paste functionality because it is much slower than directly assigning a cell's value to another cell.我不会使用复制/粘贴功能,因为它比直接将一个单元格的值分配给另一个单元格要慢得多。
Try something like this:尝试这样的事情:
Dim sourceWorksheet As Worksheet
Dim destinationWorksheet As Worksheet
Dim rangeToTransfer As Range
Set sourceWorksheet = myWorkbook.Worksheets("<name of source worksheet>")
Set destinationWorksheet = myWorkbook.Worksheets("<name of destination worksheet>")
Set rangeToTransfer = sourceWorksheet.Range("A1:C5")
Dim lastUsedRow As Integer
Dim rowIndex As Integer
Dim columnIndex As Integer
lastUsedRow = destinationWorksheet.Cells(destinationWorksheet.Rows.Count, 1).END(xlUp).row
For rowIndex = 1 To rangeToTransfer.Rows.Count
For columnIndex = 1 To rangeToTransfer.Columns.Count
destinationWorksheet.Cells(lastUsedRow + rowIndex, columnIndex).Value2 = rangeToTransfer.Cells(rowIndex, columnIndex).Value2
Next
Next
I am making an assumption here:我在这里做一个假设:
But I hope this points you in the right direction!但我希望这能为您指明正确的方向!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.