Solved

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

Posted on 2013-11-05
3
311 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 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39625322
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
ID: 39625437
Works perfect. I just need to add the formatting. Again, Thanks. Brent
0
 

Author Closing Comment

by:bvanscoy678
ID: 39625441
Thanks Brad!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

911 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

19 Experts available now in Live!

Get 1:1 Help Now