LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Junior Member
 
Location: Maine, USA
Posts: 8
Send a message via Yahoo to mainemike
Angry Excel VBA debugging help needed!

I need help writing some VBA code. Here's what I'm trying to do...

When a user closes an Excel spreadsheet (results.xls), I want to use Workbook/BeforeClose to export all the contents of all worksheets, as CSVs, as tab name.csv, to the directory that the original spreadsheet was opened from. I do not want any user interaction.

What I have so far is...

' Start of coding
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wks As Worksheet
Dim newWks As Worksheet
Dim MyPath As String
MyPath = ActiveWorkbook.Path
For Each wks In ActiveWorkbook.Worksheets
wks.Copy 'copies to a new workbook
With ActiveSheet
.Parent.SaveAs Filename:=MyPath & "\" & .Name, FileFormat:=xlCSV
.Parent.Close savechanges:=False
End With
Next wks
End Sub
' End of coding

Here are my problems so far...
1) It only saves one out of the four worksheets.
2) If the .csv file exists, it prompts to overwrite. I want it to just write over without the prompt.
3) The user ends up with a new workbook that has only the first worksheet. I'm assuming this is a result of the "wks.Copy" not 'cleaning up' correctly.


If anyone could help debug this, or give me suggestions on how to streamline the code, I would greatly appreciate it!
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2003 FAILS, but Excel 2000 SUCCEEDS ??? Richard Excel Discussion (Misc queries) 2 May 13th 23 11:46 AM
Excel Help Needed Matt Excel Worksheet Functions 1 January 5th 06 04:21 PM
Display form from an VB application in Excel kuhni Excel Discussion (Misc queries) 0 August 10th 05 06:04 PM
Is anything needed if I only load Word & Excel from Office XP Sta. Art SC Excel Discussion (Misc queries) 0 January 26th 05 01:59 AM
Difference in number of Excel NewsGroups Hari Prasadh Excel Discussion (Misc queries) 1 January 25th 05 11:32 AM


All times are GMT +1. The time now is 12:50 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"