简体   繁体   中英

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. 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. 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. This file is then used to sync data across multiple excel files.

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:

  • You know the exact range you are trying to copy - A1:C5

But I hope this points you in the right direction!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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