VBA range export to csv file

Duncan23

New Member
Joined
Jul 28, 2017
Messages
3
Hi,

I am trying to achieve two things with one macro, the first I have solved with the sub below, I now need to populate the file with only the range L2:L11 and not the rest of the data in the sheet, maintaining the rows so each cell contents are on a seperate row in the csv file:

Code:
Private Sub CommandButton1_Click()
Dim content As String
Dim rng As Range
Set rng = Range("L2:L11")
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
Dim FileName3 As String
Path = "C:\Duncans File\"
'Path = "T:\Richards Reports\MG Orders\"
FileName1 = Range("A2")
FileName2 = Range("I2")
FileName3 = Range("H2")
ActiveWorkbook.SaveAs FileName:=Path & FileName1 & "_" & FileName2 & "_" & FileName3 & ".txt", FileFormat:=xlTextMSDOS
End Sub

If anyone has a suggestion, I would much appreciate it, am just finding my way round macro's and at this time Friday afternoon, my brain is fried!

TIA,

Duncan
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Welcome to the forums!

Try:

Code:
Private Sub CommandButton1_Click()
Dim content As String
Dim rng As Range
Set rng = Range("L2:L11")
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
Dim FileName3 As String

Dim sWB     As Workbook, _
    sWS     As Worksheet
    
Dim dWB     As Workbook, _
    dWS     As Worksheet
    
Path = "C:\Duncans File\"
'Path = "T:\Richards Reports\MG Orders\"
FileName1 = Range("A2")
FileName2 = Range("I2")
FileName3 = Range("H2")
Set sWB = ActiveWorkbook
Set sWS = sWB.ActiveSheet

Set dWB = Workbooks.Add
Set dWS = dWB.Sheets(1)

sWS.Range("L2:L11").Copy Destination:=dWS.Range("a1")
dWB.SaveAs filename:=Path & FileName1 & "_" & FileName2 & "_" & FileName3 & ".txt", FileFormat:=xlTextMSDOS
End Sub
 
Last edited:
Upvote 0
Many thanks for the input, it is close but output is #REF! in each row. Is it possible to force Excel not to open the file after creation?
Thanks again, your help is much appreciated
Duncan
 
Upvote 0
It sounds like the cells you're copying over contain formulas. In this case, we can have it paste-special>values. We can also close the new workbook after it's saved.

Try:

Code:
Private Sub CommandButton1_Click()
Dim content As String
Dim rng As Range
Set rng = Range("L2:L11")
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
Dim FileName3 As String

Dim sWB     As Workbook, _
    sWS     As Worksheet
    
Dim dWB     As Workbook, _
    dWS     As Worksheet
    
Path = "C:\Duncans File\"
'Path = "T:\Richards Reports\MG Orders\"
FileName1 = Range("A2")
FileName2 = Range("I2")
FileName3 = Range("H2")
Set sWB = ActiveWorkbook
Set sWS = sWB.ActiveSheet

Set dWB = Workbooks.Add
Set dWS = dWB.Sheets(1)

sWS.Range("L2:L11").Copy
dWS.Range("A1").PasteSpecial xlPasteValues
dWB.SaveAs filename:=Path & FileName1 & "_" & FileName2 & "_" & FileName3 & ".txt", FileFormat:=xlTextMSDOS
dWB.Close False
End Sub
 
Last edited:
Upvote 0
Happy to be of assistance - thanks for the feedback. Have a great day/weekend!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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