views:

207

answers:

1

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. Driving me crazy. I'm sure this is pretty easy for those who are more advanced with VB code.
Anyone willing to help me out would have my eternal thanks!

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.
+2  A: 

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.

Ben McCormack
This is pretty awesome. Actually, they aren't in the same workbook - two separate workbooks, but I can try and figure that out. I appreciate the help!!!
valerie
@valerie. All you should need to do if the worksheets are in different workbooks is 1) Make sure to open the workbooks in the same Excel "session" so they can talk to each other (opening them both from your My Documents folder or other folder should make sure this works OK) - 2) you'll need to modify the parts of the code that say `... = Worksheets("Sheet1")` to include the Workbook, which I think is like `... = Workbooks("WorkbookSample1").Worksheets("Sheet1")`. I'm not sure of the exact syntax, but I think that's how it works. Let me know how it turns out.
Ben McCormack
Works beautifully - and that is the syntax. Genius!
valerie
@valerie FYI - if this helped you out, you can upvote my answer and/or mark it as correct.
Ben McCormack