简体   繁体   English

当我在 For 循环中复制和粘贴数据时,为什么我的数据粘贴不正确?

[英]Why isn't my data pasting correctly when I copy and paste it within a For loop?

I am trying to copy data from one sheet to another using VBA For loop.我正在尝试使用 VBA For 循环将数据从一张表复制到另一张表。 For some reason when I execute the below code the data is not being copied in one column under each other but in a strange format as shown below.由于某种原因,当我执行以下代码时,数据并没有被复制到彼此下方的一列中,而是以一种奇怪的格式复制,如下所示。

Sub Macro2()

For i = 110 To 116
For j = 1 To 1


Worksheets("overview of contracts").Activate
Range("A" & i).Select
Selection.Copy
Worksheets("Sheet1").Activate
ActiveCell.Offset(1, 0).Range("C" & j).Select
ActiveSheet.Paste

'Worksheets("overview of contracts").Activate
'Range("B" & i).Select
'Selection.Copy
'ActiveCell.Offset(1, 0).Range("D" & j).Select
'ActiveSheet.Paste

'Range("C" & i).Select
'Selection.Copy
'ActiveCell.Offset(1, 0).Range("E" & j).Select
'ActiveSheet.Paste

 Next j
Next i

End Sub

Result:结果:

在此处输入图像描述

This is the original data, what I am trying to achieve is copy that data to another sheet using a loop exactly in that format and sequence.这是原始数据,我想要实现的是使用完全按照该格式和顺序的循环将该数据复制到另一张表。 Sample Data样本数据

I think this is what you want.我想这就是你想要的。 It takes values in sheet("overview of contacts") , and copies their values to sheet("Sheet1") in column C, descending rows.它采用sheet("overview of contacts")中的值,并将它们的值复制到 C 列中的sheet("Sheet1")中,行降序。

Sub Macro3()
    Dim i As Long
    Dim j As Long
    j = 2
    For i = 110 To 116
        Worksheets("Sheet1").Cells(j, 3).Value = Worksheets("Overview of Contracts").Cells(i, 1).Value
        j = j + 1
    Next i
End Sub

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

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