开发者

Copy and Paste every Nth Row

开发者 https://www.devze.com 2023-01-04 03:49 出处:网络
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 thi

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 a开发者_开发技巧dvanced with VB code.

Copy until Column A is empty

[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.


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 Sub into your macro function, or you can paste that code at the bottom of your macro module and put a line of code Call Test inside your Macro.

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消