?
Solved

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

Posted on 2013-11-05
3
Medium Priority
?
320 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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,…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

719 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