Code check

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
hi what is wrong with this code? i have set ws as Sheet2

With ws
.Copy.SaveAs Filename:= _
"U:\TL\PV\CSV\test.csv", FileFormat:=xlCSV, _
CreateBackup:=False
.Close = False
End With
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Then the file is damaged.

I tried the code on a new workbook and still get the same results, but if i run the code from sheet 2 then it works, if i run the macro from sheet 1 then i need to put sheet2.activate at the starts, then i t works

Sub csv()
Dim ws As Worksheet
Sheet2.Activate
Set ws = Sheets("Sheet2")
ws.SaveAs "U:\TL\PV\CSV\test.csv", xlCSV
ActiveWorkbook.Close True
End Sub
 
Upvote 0
Just as a random question/note, does it matter if the sheet "programmatic name" is changed from VBE?
 
Upvote 0
I tried the code on a new workbook and still get the same results, but if i run the code from sheet 2 then it works, if i run the macro from sheet 1 then i need to put sheet2.activate at the starts, then i t works

Sub csv()
Dim ws As Worksheet
Sheet2.Activate
Set ws = Sheets("Sheet2")
ws.SaveAs "U:\TL\PV\CSV\test.csv", xlCSV
ActiveWorkbook.Close True
End Sub

Did you try what I said at #10?

ActiveWorkbook.Close False

Mark
 
Upvote 0
OK that works but i need to active workbook saved as well before closing, as i will lose data

Sorry, do you mean you want to save ThisWorkbook before doing the SaveAs (.csv)? If so:

Code:
Sub csv()
Dim ws As Worksheet
    ThisWorkbook.Save
 
    Set ws = Sheets("Sheet2")
 
    ws.SaveAs "U:\TL\PV\CSV\test.csv", xlCSV
    ActiveWorkbook.Close False
End Sub

I agree with Seiya, both that there's no reason to select/activate and that once a reference is set to the sheet, this remains explicit. The problem was that when you tossed the TRUE in behind the ActiveWorkbook.Close, you were essentially doing the same thing as when that dang irratating dialog (are you really really sure? You'll lose formatting, are you super sure???) comes up after manually saving as .csv. Now since sheet 1 happens to be active, the TRUE means you were overwriting the already saved sheet2.csv.

Does that make sense?

Mark

Edit: Hi Seiya :) I belive if you test, you'll see that the False (vs True) does indeed matter.
 
Last edited:
Upvote 0
Thanks Seiya and Mark for the explanation, it works now, just one question why is it when i have data 31/03/2009 in orignal data but when i open CSV file it changes to 3/31/2009

Sorry, do you mean you want to save ThisWorkbook before doing the SaveAs (.csv)? If so:

Code:
Sub csv()
Dim ws As Worksheet
    ThisWorkbook.Save
 
    Set ws = Sheets("Sheet2")
 
    ws.SaveAs "U:\TL\PV\CSV\test.csv", xlCSV
    ActiveWorkbook.Close False
End Sub

I agree with Seiya, both that there's no reason to select/activate and that once a reference is set to the sheet, this remains explicit. The problem was that when you tossed the TRUE in behind the ActiveWorkbook.Close, you were essentially doing the same thing as when that dang irratating dialog (are you really really sure? You'll lose formatting, are you super sure???) comes up after manually saving as .csv. Now since sheet 1 happens to be active, the TRUE means you were overwriting the already saved sheet2.csv.

Does that make sense?

Mark

Edit: Hi Seiya :) I belive if you test, you'll see that the False (vs True) does indeed matter.
 
Upvote 0

Forum statistics

Threads
1,215,241
Messages
6,123,824
Members
449,127
Latest member
Cyko

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top