Export range of data as csv file

wjeil

New Member
Joined
Feb 25, 2022
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have some data in a worksheet which i want to export as a csv file. Everything except the first row must be exported as an csv file, with a pop-up to save the file. The result must be like the second table, thus with a comma spearating the 2 columns. How do I do this? It also is a dynamic range so the last row can differ.

Config7.8.xlsm
AB
1OrderIDSKU_ID
2Order 1A2
3Order 2B26
4Order 3A1
5Order 4A9
6Order 4A20
7Order 5B16
8Order 6A3
9Order 7A24
10Order 7A20
11Order 7A19
12Order 7B26
13Order 7A1
14Order 7A6
15Order 8A11
16Order 9A13
Input_File


Cell Formulas
RangeFormula
E1:E15E1=A1&","&B1


I would really appreciate if someone can help me out.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
VBA Code:
Sub Create_Txt_File()

myfile = "c:\test\test.csv"
If Dir(myfile) <> "" Then Kill myfile ' deletes file if it exists

For r = 2 To Cells(Rows.Count, "A").End(xlUp).Row
    lcol = Cells(r, 256).End(xlToLeft).Column
    For c = 1 To lcol
        delim = ""
        If c < lcol Then delim = ","
        data = data & Cells(r, c) & delim
    Next c
        data = data & vbLf
 Next r
   
Open myfile For Append As #1
Print #1, data
Close #1
data = ""

End Sub
 
Upvote 0
Thanks! Only 1 adjustment, I only want to save column A and B (there are more columns active in C,D and E). Now they are all included. And how can i choose the save location with a pop-up screen? So that the directory can be chosen.?

Thanks in advance!
 
Upvote 0
Hi, first that no needs to use Blad1 worksheet …​
What is strange according to your expected csv text file is there is no header within, really ?​
 
Upvote 0
So you can delete the useless Blad1 worksheet and use this VBA demonstration as a starting point :​
VBA Code:
Sub Demo1()
    Dim V, F%, R&
        V = Application.GetSaveAsFilename(, "Text files,*.csv", , "File to save"):  If V = False Then Exit Sub
        F = FreeFile
        Open V For Output As #F
    With [Input_File!A1].CurrentRegion.Columns("A:B").Rows
        For R = 2 To .Count:  Print #F, Join(Application.Index(.Item(R).Value2, 1, 0), ","):  Next
    End With
        Close #F
End Sub
 
Last edited:
Upvote 0
Solution
wjeil give this a try. it allows you to change how many columns you want.

it allows the user to select the save to folder

VBA Code:
Sub Create_Txt_File_2Columns()

    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then Exit Sub
        sItem = .SelectedItems(1)
    End With


myfile = sItem & "\test.csv" 'adjust this name as needed

If Dir(myfile) <> "" Then Kill myfile ' deletes file if it exists

For r = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    lcol = 2 'number of columns wanted
   
    For c = 1 To lcol
        delim = ""
        If c < lcol Then delim = ","
        data = data & Cells(r, c) & delim
    Next c
        data = data & vbLf
 Next r
  
Open myfile For Append As #1
Print #1, data
Close #1
data = ""

End Sub
 
Upvote 0
As a reminder Print #F, Join(Application.Index(.Item(R).Value2, 1, 0), ",") is universal whatever the Excel version​
but as recent versions have the TEXTJOIN worksheet function so it could be Print #F, Application.Textjoin(",", False, .Item(R)) …​
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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