简体   繁体   English

Excel VBA 通过迭代复制和粘贴值

[英]Excel VBA to copy and paste values with iterations

My problem is the following: I have some data in an Excel sheet (1 column, multiple rows) which I want to copy and paste to a new sheet (sheet2) still in a row.我的问题如下:我在 Excel 工作表(1 列,多行)中有一些数据,我想将其复制并粘贴到仍然连续的新工作表(sheet2)中。 Values are changing all the time (Montecarlo simulations) so I need to introduce some iterations as well.值一直在变化(Montecarlo 模拟),所以我还需要引入一些迭代。

This is my code:这是我的代码:

Sheets("sheet1").Range("a2:z2").Copy
With Sheets("sheet2")
For i = 1 To 10000
    .Range("A" & i + 1 & ":B" & i + 1 & ":C" & i + 1 & ":D" & i + 1 & ":E" & i + 1 & ":F" & i + 1 & ":G" & i + 1 & ":H" & i + 1 & ":I" & i + 1 & ":J" & i + 1 & ":K" & i + 1 & ":L" & i + 1 & ":M" & i + 1 & ":N" & i + 1 & ":O" & i + 1 & ":P" & i + 1 & ":Q" & i + 1 & ":R" & i + 1 & ":S" & i + 1 & ":T" & i + 1 & ":U" & i + 1 & ":V" & i + 1 & ":W" & i + 1 & ":X" & i + 1 & ":Y" & i + 1 & ":Z" & i + 1 & ":AA" & i + 1 & ":AB" & i + 1 & ":AC" & i + 1 & ":AD" & i + 1 & ":AE" & i + 1 & ":AF" & i + 1).PasteSpecial Paste:=xlPasteValues
Next i
End With

This is of course not flexible at all since i need to specify precisely how many values I have Range("a2:z2") and I need to write all the letters corresponding to the paste columns (A to AF)这当然根本不灵活,因为我需要精确指定我有多少个值 Range("a2:z2") 并且我需要写下与粘贴列相对应的所有字母(A 到 AF)

What I would like to achieve is the following: -) Can I copy values from a column instead of a row?我想要实现以下目标:-) 我可以从列而不是行中复制值吗? for example sheet 1 column A -) Can I have the VBA to check how many values there are instead of me having to specify it ?例如表 1 列 A -) 我可以让 VBA 检查有多少值而不是我必须指定它吗? maybe soemetimes there are 10 and sometimes 50 -) When I paste values in sheet 2 is there a way where I do not need to specify all column names but just have excel figure it out itself?有时可能有 10 个,有时有 50 个 -) 当我在工作表 2 中粘贴值时,有没有一种方法可以让我不需要指定所有列名,而只需让 excel 自己弄清楚?

The task is basically the following: -) Count how many values are there in column A (or whatever column contains raw data) -) Copy those values -) Paste values in first available row in sheet 2 -) Specify number of iterations -) copy and past N-times corresponding to number of iterations so I have as many output rows as iterations该任务基本上如下:-)计算A列(或包含原始数据的任何列)中有多少值-)复制这些值-)将值粘贴到工作表2的第一个可用行中-)指定迭代次数-)复制和过去 N 次对应于迭代次数,所以我有与迭代一样多的输出行

I am sure it should be fairly easy with a simple code, but I could not find it out myself Thank you very much beforehand我相信用一个简单的代码应该很容易,但我自己找不到它 非常感谢你

Use the .PasteSpecial property to transpose your copy selection.使用.PasteSpecial属性转置您的副本选择。

    Dim lcol As Long
    Dim j As Integer 'Number of iterations
    Dim searchRange As Range
    Dim transposeRange As Range
    'Find the last value in the column
    lcol = Range("A:A").SpecialCells(xlCellTypeLastCell).Row
    j = 'Set your iteration number

    For i = 1 To j 'Count through all iterations
        Sheet1.Range("A1:A" & lcol).Copy
        'Transpose the selection to paste into row
        Sheet2.Rows(i).PasteSpecial transpose:=True
    Next i

If you don't know the number of iterations, call this as a sub in the Worksheet.Change event, which will run every time the values in the column change.如果您不知道迭代次数,请将其作为Worksheet.Change事件中的子调用,该事件将在每次列中的值更改时运行。

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

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