Saving one sheet as CSV and naming sheet based on cell value

DanaS85

New Member
Joined
Apr 16, 2020
Messages
3
Office Version
  1. 2010
Hi

I'm very new to VBA and very stuck on a VBA code and I hope you can help.

I have a workbook, with multiple sheets, where one sheet named "Label" needs to be exported as a UTF8 csv file to a specific folder. At the same the sheet is to be named based on a cell value. The csv is to be used in Illustrator.

I've tried the following VBA that does all of the above, but won't load in Ilustrator:

Sub sb_Copy_Save_Worksheet_As_Workbook()
Application.DisplayAlerts = False
Dim strSourceSheet As String
Dim strFullname As String

strSourceSheet = "Label"

ThisWorkbook.Sheets(strSourceSheet).Copy
ActiveWorkbook.SaveAs Filename:=("C:\test\") & Range("B2").Text, _
FileFormat:=xlCSVUTF8, _
CreateBackup:=False
ActiveWorkbook.Close
Application.DisplayAlerts = True

End Sub

But when I use the following VBA, it will load in Illustrator, but then the sheet can only be named test.

Sub sb_Copy_Save_Worksheet_As_Workbook()
Application.DisplayAlerts = False
Dim strSourceSheet As String
Dim strFullname As String

strSourceSheet = "Label"
strFullname = "C:\test\test.csv"

ThisWorkbook.Sheets(strSourceSheet).Copy
ActiveWorkbook.SaveAs Filename:=strFullname, _
FileFormat:=xlCSVUTF8, _
CreateBackup:=False
ActiveWorkbook.Close
Application.DisplayAlerts = True

End Sub

I can't figure out why one VBA works and the other does not, when they are both saved as a UTF8csv. Is it because, when I use the second VBA i name the file test.csv?

Hope it makes sense.

Kind regards, Dana
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I think I found the problem, but have no idea how to fix it.

With the second VBA code, the file is saved with .CSV instead of .csv. When I use excel 2010, it saves with lowercase exstension, however using excel 2013, saves with uppercase. Is there any way to fix that, since it is only possible to save as csvUTF8 is in 2013?
 
Upvote 0
Well I think I solved it. I changed the extension to lowercase (.csv) manually on the saved file in the specific folder, and when I ran the code again, it replaced the previous file, but retained the lowercase extension.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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