VBA macro to export specific range of cells to CSV

branys

New Member
Joined
Mar 23, 2016
Messages
1
Hey everyone,

I am trying to export specific range of cells from excel sheet to CSV. In my case it should start from Row 3 and stop with last row and columns should be A:G

I am pretty amateurish with VBA so most of things I google and compile yet, but im stuck at this moment.

Ive got below macro but it exports whole sheet and I need to skip first 2 rows and stop at column G.

Thank you guys in advance

Sub CopyToCSV()
Dim MyPath As String
Dim MyFileName As String
'The path and file names:
MyPath = "C:\Temp"
MyFileName = "MyFileName" & Format(Date, "ddmmyy")
'Makes sure the path name ends with "\":
If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
'Makes sure the filename ends with ".csv"
If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
'Copies the sheet to a new workbook:
Sheets("upload").Copy
'The new workbook becomes Activeworkbook:
With ActiveWorkbook
'Saves the new workbook to given folder / filename:
.SaveAs Filename:= _
MyPath & MyFileName, _
FileFormat:=xlCSV, _
CreateBackup:=False
'Closes the file
.Close False
End With
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I just changed it so that the rows above your data and the columns to the right were deleted and not saved with the csv file.

Code:
Sub CopyToCSV()
  Dim MyPath As String
  Dim MyFileName As String
  
  'The path and file names:
  MyPath = "C:\Temp"
  MyFileName = "MyFileName" & Format(Date, "ddmmyy")
  'Makes sure the path name ends with "\":
  If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
  
  'Makes sure the filename ends with ".csv"
  If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
  
  
  Sheets("Sheet1").Copy                           'Copies the sheet to a new workbook:
  ActiveSheet.Range("1:2").EntireRow.Delete       'Delete the first two rows
  ActiveSheet.Range("H:Z").EntireColumn.Delete    'Delete any data in columns past G (if that is an issue)
  
  Application.DisplayAlerts = False               'Used to overwrite the file without notice
  With ActiveWorkbook                             'The new workbook becomes Activeworkbook:
    'Saves the new workbook to given folder / filename:
    .SaveAs Filename:=MyPath & MyFileName, FileFormat:=xlCSV, CreateBackup:=False
    'Closes the file
    .Close False
  End With
  
  Application.DisplayAlerts = True
 End Sub
 
Upvote 0

Forum statistics

Threads
1,215,366
Messages
6,124,516
Members
449,168
Latest member
CheerfulWalker

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