VBA code help to save exported file using the information in a cell

STIRRELL

Board Regular
Joined
Dec 30, 2010
Messages
63
Office Version
  1. 365
Hi,
I am not sure how to do this. I have code that is exporting a spreadsheet as a CSV file, but I would like to name the file based on information in Cell J2 on a spreadsheet within the workbook called Int_Check. Can you please help me modify the code to rename the exported file. I would like if possible the spreadsheet to stay "20" in the file. But if that is difficult I can work around this.

Thank you so much for your help


Dim csvFile As String
Dim wsName As Variant
'Dim OutApp As Object, OutMail As Object

Sheets("20").Activate

wsName = ActiveSheet.Name
csvFile = ThisWorkbook.Path & "\" & wsName & ".csv"
ThisWorkbook.ActiveSheet.Copy
ActiveWorkbook.SaveAs csvFile, FileFormat:=xlCSV
ActiveWorkbook.Close False
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this

VBA Code:
Sub Save_20()
    Dim FileName As String
    FileName = Worksheets("Int_Check").Range("J2").Value
    Worksheets("20").Copy
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & FileName & ".csv", FileFormat:=6
    ActiveSheet.Name = "20"
End Sub
 
Upvote 0
Solution
I would like if possible the spreadsheet to stay "20" in the file.
A csv file doesn't have the concept of sheets. When you open it in Excel it will always appear as a spreadsheet with 1 sheet and that sheet name will be the filename.
 
Upvote 0
A csv file doesn't have the concept of sheets. When you open it in Excel it will always appear as a spreadsheet with 1 sheet and that sheet name will be the filenam
Try this

VBA Code:
Sub Save_20()
    Dim FileName As String
    FileName = Worksheets("Int_Check").Range("J2").Value
    Worksheets("20").Copy
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & FileName & ".csv", FileFormat:=6
    ActiveSheet.Name = "20"
End Sub
 
Upvote 0
Try this

VBA Code:
Sub Save_20()
    Dim FileName As String
    FileName = Worksheets("Int_Check").Range("J2").Value
    Worksheets("20").Copy
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & FileName & ".csv", FileFormat:=6
    ActiveSheet.Name = "20"
End Sub
Thank you that worked perfectly :)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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