vba 复制并粘贴每第 N 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3094486/
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-11 11:46:01  来源:igfitidea点击:

Copy and Paste every Nth Row

excelvbaexcel-vba

提问by valerie

I need a macro that will copy a column from one worksheet and paste into another spreadsheet on every 5th row. I've tried using snippets of code, but I haven't found one that I can modify to make this work. It's driving me crazy. I'm sure this is pretty easy for those who are more advanced with VB code.

我需要一个宏,它将从一个工作表中复制一列并粘贴到每第 5 行的另一个电子表格中。我试过使用代码片段,但我还没有找到可以修改以使其工作的代码片段。这让我疯狂。我相信这对于那些使用 VB 代码更高级的人来说非常容易。

Copy until Column A is empty

复制直到A列为空

[Workbook1]
Column A
Name1
Name2
Name3
Name4
etc.



[Workbook2]
Column A
Paste Name1 in Row 5
Paste Name2 in Row 10
Paste Name3 in Row 15
Paste Name4 in Row 20
etc.

回答by Ben McCormack

Can we assume that both worksheets are in the same workbook? If so, it might look like the following code:

我们可以假设两个工作表都在同一个工作簿中吗?如果是这样,它可能类似于以下代码:

Sub Test()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lngRowCounter As Long
Dim lngRowMultiplier As Long


lngRowCounter = 1
lngRowMultiplier = 5

Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")

Do While Not IsEmpty(ws1.Range("A" & lngRowCounter))
  ws2.Range("A" & lngRowCounter * lngRowMultiplier).Value = _
    ws1.Range("A" & lngRowCounter).Value
  lngRowCounter = lngRowCounter + 1
Loop

End Sub

You can either paste the contents of the Subinto your macro function, or you can paste that code at the bottom of your macro module and put a line of code Call Testinside your Macro.

您可以将 的内容粘贴Sub到宏函数中,也可以将该代码粘贴到宏模块的底部并将一行代码Call Test放入宏中。

I hope this helps.

我希望这有帮助。