Macro to Export Sheet as CSV

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,567
Office Version
  1. 2021
Platform
  1. Windows
I have code to export a sheet as a csv

I will import one file at a time and selectb the macto to export sheet as a csv


I would like the csv filename to be be the same as the text in A6 followed by .csv for e.,g. WLTI.csv

I will import one file at a time , select the macro to export sheet as a csv and then import the next week

Your assistance in amending my code is most appreciated

Code:
 Sub Export_Imported_Sheet_As_CSV()

Application.DisplayAlerts = False

Dim Sht As Worksheet

Set Sht = Worksheets("Statement")

Sht.Copy

ActiveWorkbook.SaveAs Filename:="C:\extract\BRTI.CSV", FileFormat _

:=xlCSV

ActiveWorkbook.Close



Application.DisplayAlerts = True

end Sub


 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Good day...

Try:

VBA Code:
Sub Export_Imported_Sheet_As_CSV()
    Application.DisplayAlerts = False
    Dim Sht As Worksheet
    Set Sht = Worksheets("Statement")
    Sht.Copy
    ActiveWorkbook.SaveAs Filename:="C:\extract\" & Range("A6").Value & ".csv", FileFormat _
    :=xlCSV
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
Hi Jimmypop

i need one more change. I want to savee the file with the name pertaining to text on sheets "Codes" in D1

D1 has a formula

i get a subscript if range

Code:
   ActiveWorkbook.SaveAs Filename:="C:\Pull\" & Sheets("Codes").Range("D1").Value & ".csv", FileFormat _
    :=xlCSV


Kindly amend my code
 
Upvote 0
ActiveWorkbook.SaveAs Filename:="C:\Pull\" & Sheets("Codes").Range("D1").Value & ".csv", FileFormat _ :=xlCSV

Try:

VBA Code:
ActiveWorkbook.SaveAs Filename:="C:\Pull\" & Worksheets("Codes").Range("D1").Value & ".csv", FileFormat _
    :=xlCSV
 
Upvote 0
Unfortunately I still get Subscript out of range

Please check & advise
 
Upvote 0
Mmmmm...ok try following approach: (Hope I get it right seeing as I also still learning):

VBA Code:
Private Sub CommandButton1_Click()
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = wb.Codes ' you can change this to reference the sheet where your save name lives.
    SaveName = ws.Range("D1") 'Change this to wherever you want to pull the save name
    ActiveWorkbook.SaveAs Filename:="C:\Pull\" _
    & SaveName & ".csv", FileFormat _
    :=xlCSV
End Sub
 
Upvote 0
Unfortunately I still get Subscript out of range

Please check & advise

Please ignore:

Mmmmm...ok try following approach: (Hope I get it right seeing as I also still learning):

VBA Code:
Private Sub CommandButton1_Click()
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = wb.Codes ' you can change this to reference the sheet where your save name lives.
    SaveName = ws.Range("D1") 'Change this to wherever you want to pull the save name
    ActiveWorkbook.SaveAs Filename:="C:\Pull\" _
    & SaveName & ".csv", FileFormat _
    :=xlCSV
End Sub

And use:

VBA Code:
Private Sub CommandButton1_Click()
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = wb.Sheets("Codes") ' you can change this to reference the sheet where your save name lives.
    SaveName = ws.Range("D1") 'Change this to wherever you want to pull the save name
    ActiveWorkbook.SaveAs Filename:="C:\Pull\" _
    & SaveName & ".csv", FileFormat _
    :=xlCSV
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,606
Messages
6,125,803
Members
449,261
Latest member
Rachel812321

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