Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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 how to use a scrolling table in Microsoft Excel using the INDEX function.

828 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