简体   繁体   English

如何复制和粘贴到列?

[英]How can I copy&paste to columns?

I have made some model in Excel, where I first have to manually copy values from H24:H25 and special paste it to B29:B30 and then I have to copy values from B29:B30 to the table with this range: G30:M31, but in first loop I need to paste it to G30:G31 (it is 1st year), in second to H30:31 ... and in last loop i need to paste from B29:30 to M30:M31 (7th year) 我在Excel中制作了一些模型,我首先必须手动复制H24中的值:H25并将其特殊粘贴到B29:B30然后我必须将B29:B30中的值复制到具有此范围的表格:G30:M31,但是在第一个循环中我需要将它粘贴到G30:G31(它是第1年),在第二个循环中粘贴到H30:31 ......并且在最后一个循环中我需要从B29:30粘贴到M30:M31(第7年)

Sub MAKRO()
    Dim Year As Integer
    For Year = 1 To 7
    Range("K45") = Year
    Range("H24:H25").Select
    Selection.Copy
    Range("B29:B30").Select
    Selection.PasteSpecial Paste:=xlPasteValues
    Range("B29:B30").Select
    Selection.Copy
    Range(I cant figure out this part)
    Selection.PasteSpecial Paste:=xlPasteValues
    Next Year
End Sub

A better way of writing this would be something like the following: 更好的写作方式如下:

Dim WS as worksheet

Set WS = Thisworkbook.sheets("Sheet Name")
    For i = 1 to 7
        WS.Range(YearRange) = i
        WS.Range(DestinationRange).Offset(0,I).value2 = WS.Range(SourceRange).value2
    Next i

The rest (and how exactly to use offset) is left as an exercise for the reader... (Aka I'm quickly freehanding this code) 剩下的(以及如何使用偏移量)留给读者练习......(Aka我很快就自由地编写了这段代码)

Please note that you do need to fill in the destination range (like A1:A2) 请注意,您需要填写目的地范围(如A1:A2)

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

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