Excel Macro
Posted by Caroline Andrews on October 16, 2001 5:15 AM
I have been working with a macro that is supposed to pull 2 worksheets out of a series of workbooks in a specific directory and save them as a text file. It is not working, and since I am new at Macro's I was wondering if anyone could help. I get an error at the line I indicated. Here is the macro:
Sub test()
' Loop through the files specified in mypath.
myPath = "C:\OCDM*.xls"
MyName = Dir(myPath)
Do While MyName <> ""
' extract the path and name of the file minus the .xls
nameroot = Left(MyName, Len(MyName) - 4)
****here is where I get the error:
Workbooks.Open MyName
Workbooks(MyName).Activate
' save the first two worksheets to text files
ActiveWorkbook.Worksheets(1).SaveAs nameroot & "_sum.txt", xlTextMSDOS
ActiveWorkbook.Worksheets(2).SaveAs nameroot & "_tmp.txt", xlTextMSDOS
'Close the active workbook
Workbooks(ActiveWorkbook.Name).Close SaveChanges:=False
'retrieve the next xls file
MyName = Dir
Loop
End Sub