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("E
xport")
Set fso = CreateObject("Scripting.Fi
leSystemOb
ject")
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.SelectedShee
ts.Delete
wsSource.Copy After:=.Sheets(.Sheets.Cou
nt)
Set wsDest = .Worksheets(.Worksheets.Co
unt)
'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.SelectedSheet
s.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 Sub
Start Free Trial