Advertisement

05.09.2008 at 06:52AM PDT, ID: 23389279
[x]
Attachment Details

Excel 2007 Macro to Replace Sheets In Multiple Workbooks

Asked by pbait in Microsoft Excel Spreadsheet Software, Spreadsheet Software

Tags: , , ,

I have a large number of spreadsheets with a faulty sheet in them (link is broken).  I'd like to use a macro to do the following thing:

1. Open each "broken" spreadsheet from a common location.
2. Delete the "bad" sheet (which is hidden, so I don't know if needs to be unhidden first).
3. Replace the "bad" sheet with the "good" one from the spreadsheet hosting the macro I'm talking about.
4. Delete the link reference to the workbook being copied from.
5. Hide the sheet.
6. Select another sheet and go to cell A1.
7. Save the workbook - HOWEVER, the workbooks are currently in 2003 format, and I'd like to save them as 2007 xlsx files (with no macros).

I've got the beginnings of a macro to do this, but need some of the finer points sorted out.  Specifically, I'm not sure about exact steps to unhide and delete the current "bad" sheet, nor how to save the workbook with its current name, but in the .xlsx format.  For reference, the name of the "bad" sheet is 'Export' and the sheet I want to end up on is 'Yield'.

Sub CopySheet2()
   
    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim wb As Workbook
    Dim cel As Range
    Dim fso As Object
    Dim fld As Object
    Dim fil As Object
    Dim BeforeBrak As String
    Dim AfterBrak As String
   
        Set wsSource = ThisWorkbook.Worksheets("Export")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder("g:\ppw\test\")
    For Each fil In fld.Files
        Set wb = Workbooks.Open(fil.Path)
        With wb
            'Sheets("Export").Select 'Select the existing Export sheet if there is one - but it's hidden
            'ActiveWindow.SelectedSheets.Delete
            wsSource.Copy After:=.Sheets(.Sheets.Count)
            Set wsDest = .Worksheets(.Worksheets.Count)

'Replace link created by copying the sheet from one workbook to another. Format is .xlsm because it's macro enabled

            Cells.Select
            Selection.Replace What:="y:\[fix.xlsm]", _
        Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
        False, SearchFormat:=False, ReplaceFormat:=False

'Hide the Export Sheet and move to cell A1 of the Yield sheet

            Sheets("Export").Select
            ActiveWindow.SelectedSheets.Visible = False
            Sheets("Yield").Select
            Range("A1").Select

'Not sure what to do here to make it SaveAs in the new format

            .Save
            .Close
        End With
    Next
   
    Set fil = Nothing
    Set fld = Nothing
    Set fso = Nothing
   
    MsgBox "Done"
   
End SubStart Free Trial
[+][-]05.09.2008 at 09:04AM PDT, ID: 21534240

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Excel Spreadsheet Software, Spreadsheet Software
Tags: Microsoft, Excel, 2007, Macros
Sign Up Now!
Solution Provided By: rorya
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.09.2008 at 11:14AM PDT, ID: 21535250

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.09.2008 at 12:26PM PDT, ID: 21535768

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.12.2008 at 09:04AM PDT, ID: 21548128

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 01:43AM PDT, ID: 21553177

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.13.2008 at 04:12AM PDT, ID: 21553806

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628