vba 使用VBA复制一行中的几个值并将其多次粘贴到excel的同一行中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22510230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:24:57  来源:igfitidea点击:

copying few values in a row and pasting it multiple times in the same row in excel using VBA

excelvbaexcel-vba

提问by vinoth mannan

I am very new to VBA.I know its a basic doubt but couldnt find the solution anywhere. I am trying to copy 24 (A1:A24)values and paste it number of times in (A25:A8760).I am able to copy a single value and paste it number of times.I couldnt do it for multiple values.

我对 VBA 很陌生。我知道这是一个基本的疑问,但在任何地方都找不到解决方案。我正在尝试复制 24 个 (A1:A24) 值并将其粘贴到 (A25:A8760) 中。我能够复制单个值并粘贴多次。我无法为多个值执行此操作。

Sub Macro1()

Range("a1").Select
Selection.Copy

Range("a2:a30").Select

ActiveSheet.Paste     

End Sub

How do i do it if i multiple values?

如果我有多个值,我该怎么做?

Sub Macro1()

Range("a1:a24").Select
Selection.Copy

Now i need to paste the above 24 values to (A25:A8760) continously? Thanks

现在我需要将上述 24 个值连续粘贴到 (A25:A8760) 中?谢谢

采纳答案by Dmitry Pavliv

At first I thought this: Range("A1:A24").Copy Range("A25:A8760")or Range("A25:A8760").Value = Range("A1:A24").Valueshould work. However it doesn't, because above solutions copy only in "used" part of the sheet.

起初我是这样想的:Range("A1:A24").Copy Range("A25:A8760")还是Range("A25:A8760").Value = Range("A1:A24").Value应该工作。然而事实并非如此,因为上述解决方案仅在工作表的“已使用”部分复制。

Then I come up with following solution (copies only values):

然后我想出了以下解决方案(仅复制值):

With Range("A25:A8760")
    .Formula = "=A1"
    .Value = .Value
End With

UPD:

更新:

this code works:

此代码有效:

Range("A25:A8760").Value = ""
Range("A1:A24").Copy Range("A25:A8760")

Notes:

笔记:

I used Range("A25:A8760").Value = ""to make this range to be part of UsedRange(otherwise line Range("A1:A24").Copy Range("A25:A8760")would copy only in "used" part of the sheet)

我曾经Range("A25:A8760").Value = ""使这个范围成为其中的一部分UsedRange(否则行Range("A1:A24").Copy Range("A25:A8760")只会在工作表的“使用”部分复制)

回答by Wayne G. Dunn

The following will copy and past 364 times...

以下将复制和过去 364 次...

Sub Macro1()
Dim i As Integer

Range("A1:A24").Select
Selection.Copy
For i = 1 To 364
    Range("A" & 1 + i * 24).Select
    ActiveSheet.Paste
Next i
End Sub