VBA Export Range to CSV Help

kylemr

New Member
Joined
Jan 3, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to save a range from a specific worksheet within a macro enabled workbook. I would like to export the range, leaving all data intact after saving. I would also like to save the csv in the same location as the workbook.

I've got this far on my VBA script so far, can anyone please assist? It's not working at all


VBA Code:
Sub SaveAsCSV()
   
    Dim wb As Workbook
    Dim nam As String
   
    Application.DisplayAlerts = False
    With Worksheets("Export")
        nam = .Range("A1")
        .Range("A4:T1000").Copy
    End With
    Set wb = Workbooks.Add
    With wb
    ActiveSheet.PasteSpecial
   
    Sub ExampleToSaveWithSamePathDifferentName()
    Dim sFilename As String
    sFilename = "WorkbookName.xls" 'You can give a nem to save

    Workbooks.Add
    'Saving the Workbook
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & sFilename

    End Sub
 
Last edited by a moderator:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try
VBA Code:
Sub SaveAsCSV()
    Dim wb As Workbook
    Dim nam As String
    Application.DisplayAlerts = False
    With Worksheets("Export")
        nam = .Range("A1")
        .Range("A4:T1000").Copy
    End With
    Set wb = Workbooks.Add
    With wb
        .SaveAs Filename:=ThisWorkbook.Path & "\" & nam, FileFormat:=xlCSV, CreateBackup:=True
        .Close
    End With
    ThisWorkbook.Close False
End Sub
 
Upvote 0
Solution
Try
VBA Code:
Sub SaveAsCSV()
    Dim wb As Workbook
    Dim nam As String
    Application.DisplayAlerts = False
    With Worksheets("Export")
        nam = .Range("A1")
        .Range("A4:T1000").Copy
    End With
    Set wb = Workbooks.Add
    With wb
        .SaveAs Filename:=ThisWorkbook.Path & "\" & nam, FileFormat:=xlCSV, CreateBackup:=True
        .Close
    End With
    ThisWorkbook.Close False
End Sub
It returned an error that just said '400'

Any thoughts?
 
Upvote 0
Tested ok in here
gave same error when
Range("A1") in sheets("Export") is not empty
 
Upvote 0
Does that mean that cell A1 must be empty? or must be populated? sorry for the basic questions I am a beginner with VBA (obviously)
 
Upvote 0
Must be populated with the file name you want to save the csv file with
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,105
Members
449,066
Latest member
Andyg666

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