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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Im now getting a runtime error, when I debug the line below is highlighted yellow:

Rich (BB 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
 
Last edited by a moderator:
Upvote 0
Thanks for your script. Error message now says script out of range
 
Upvote 0
REplace with
VBA Code:
  .Sheets("Sheet1").Range("A4").Resize(UBound(a), UBound(a, 2)) = a
an extra s deleted
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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