View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Ron de Bruin Ron de Bruin is offline
external usenet poster
 
Posts: 11,123
Default Export Data to CSV without closing source workbook

Typo in my code line

'If you want to close the csv file use
'wb.Close flase


flase must be False

Sorry


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Tom Ogilvy" wrote in message ...
Private Sub CreateUploadFile_Click()
Dim FName As String
Dim wb As Workbook
FName = "Upload" & Format(Now(), "yyyymmmddhhmm")
Sheets("Upload Data").Copy
Set wb = ActiveWorkbook
wb.SaveAs FName & ".csv", FileFormat:=xlCSV
wb.Close flase SaveChanges:=False
MsgBox "Save File Complete"
End Sub

The changes have already been saved, so saying false prevents saving it again. If you don't trust it, specify true. In any
event, you won't be prompted fi you add the argument.

--
Regards,
Tom Ogilvy


"Connie" wrote in message oups.com...
Ron: You code worked beautifully! I can't thank you enough. The only
issue is that when I close the file, I get the following prompt:

"Do You Want to Save the Changes You Made To FileName.csv". How would
I include that in the code so it automatically saves the changes. It's
strange that I get the prompt, as no changes were made.

Here's the code I'm using:

Private Sub CreateUploadFile_Click()
Dim FName As String
Dim wb As Workbook
FName = "Upload" & Format(Now(), "yyyymmmddhhmm")
Sheets("Upload Data").Copy
Set wb = ActiveWorkbook
wb.SaveAs FName & ".csv", FileFormat:=xlCSV
wb.Close flase
MsgBox "Save File Complete"
End Sub

Also, when the file is closed, the program automatically returns to the
sheet where the command button was launched (which is exactly what I
want). Is that because the Sheets("Upload Data").Copy command doesn't
actually select the sheet? In my prior code, I used Sheets("Upload
Data").Select to select the sheet before I did the save. This
positioned me on the data sheet after the save which is not what I
wanted. I like your code better! I'm learning!

Ron de Bruin wrote:
Try this Connie

Private Sub CreateUploadFile_Click()
Dim FName As String
Dim wb As Workbook
FName = "Upload" & Format(Now(), "yyyymmmddhhmm")
Sheets("Upload Data").Copy
Set wb = ActiveWorkbook
wb.SaveAs FName & ".csv", FileFormat:=xlCSV
MsgBox "Save File Complete"

'If you want to close the csv file use
'wb.Close flase
End Sub



--
Regards Ron de Bruin
http://www.rondebruin.nl



"Connie" wrote in message oups.com...
I have a workbook which is used by payroll clerks to input time sheet
data for employees. Based on the input, the spreadsheet performs
calculations on a separate sheet, and I would like to export that data
to a csv file. I created a command button to do this which contains
the following code. When the command is run, the CSV file is created;
however, the original workbook is closed and the user has to reopen it
to continue working. Is there any way to export data from a sheet in a
workbook to a CSV file without closing the original workbook?

Private Sub CreateUploadFile_Click()
Sheets("Upload Data").Select
Dim FName As String
FName = "Upload" & Format(Now(), "yyyymmmddhhmm")
ActiveSheet.Copy
ActiveSheet.SaveAs FName & ".csv", FileFormat:=xlCSV
MsgBox "Save File Complete"
End Sub

Any help would be great!

CJM