Solved

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

Posted on 2013-11-05
3
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

738 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