Solved

Error with Copy/paste special values into new workbook with loop

Posted on 2013-11-05
3
310 Views
Last Modified: 2013-11-05
Hi,

It's a pretty straightforward macro (Module 2). I am looking through a list of names in my first worksheet, then copying the 2nd worksheet into a new workbook.

this code works, but it only copies the information, so I get errors when opening the new files because the links don't update.

Sub Simpleo()
Dim i As Integer
Dim owb As Workbook
Dim ws As Worksheet
Set ws = Sheet2
 
    For i = 2 To Sheet1.Range("E" & Rows.Count).End(xlUp).Row
        Sheet2.[e3] = Sheet1.Range("E" & i) & "-#1"
        Set owb = Workbooks.Add
        ws.Columns("A:H").Copy [a1]
        owb.SaveAs "C:\TEST Fitness Results\" & [e3] & ".xls"
        owb.Close False
  Next i
End Sub

Open in new window


Instead, I am trying to modify it so it will paste values instead of just copying.

I don't think I even need to copy it, but I am a bit confused with the loop and the new workbook.

Sub Simpleo()
Dim i As Integer
Dim owb As Workbook
Dim ws As Worksheet
Set ws = Sheet2
 
    For i = 2 To Sheet1.Range("E" & Rows.Count).End(xlUp).Row
        Sheet2.[e3] = Sheet1.Range("E" & i) & "-#1"
        Set owb = Workbooks.Add
        ws.Columns("A:H").Copy
        owb.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
        owb.SaveAs "C:\TEST Fitness Results\" & [e3] & ".xls"
        owb.Close False
  Next i
End Sub

Open in new window

0
Comment
Question by:bvanscoy678
  • 2
3 Comments
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
In statement 11 of your second sub, you need to specify the worksheet.
In statement 13, you need to specify the FileFormat parameter. You may also want to specify which sheet is furnishing the value of E3 for the filename.
Sub Simpleo()
Dim i As Integer
Dim owb As Workbook
Dim ws As Worksheet
Set ws = Sheet2
 
    For i = 2 To Sheet1.Range("E" & Rows.Count).End(xlUp).Row
        Sheet2.[E3] = Sheet1.Range("E" & i) & "-#1"
        Set owb = Workbooks.Add
        ws.Columns("A:H").Copy
        owb.Worksheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
        owb.SaveAs "C:\TEST Fitness Results\" & ws.[E3] & ".xls", FileFormat:=56
        owb.Close False
  Next i
End Sub

Open in new window

0
 

Author Comment

by:bvanscoy678
Comment Utility
Works perfect. I just need to add the formatting. Again, Thanks. Brent
0
 

Author Closing Comment

by:bvanscoy678
Comment Utility
Thanks Brad!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now