Modify VBCode that creates CSV text file

kmham

New Member
Joined
Mar 6, 2008
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Excel 2013
Layout:
Cell A1: stores the network drive letter (currently set as P:)
Cell A2: stores the name of the file (currently set as Test.txt)
Row 4 has column headings: 1st Value, 2nd Value, 3rd Value
Rows 5 to forever, but only Columns A, B & C have data in them. Currently I just have 1-2-3 in row 5, then 4-5-6 in row 6 and so on.
When I push the button with macro assigned to it, the data exports to the txt file as follows:
1,2,3,
4,5,6,
7,8,9,
10,11,12,

Here's the current VB Code:
Sub test()
Range("A5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "General"
Selection.HorizontalAlignment = xlLeft
Range("B5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "General"
Selection.HorizontalAlignment = xlLeft
Range("C5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "General"
Range("A5").Select
Dim i As Long, r As Range, temp As String, txt As String
With Range("a5", Range("a" & Rows.Count).End(xlUp)).Resize(, 4)
For i = 1 To .Rows.Count
For Each r In .Rows(i).Cells
temp = temp & "," & r.Text
Next
txt = txt & vbCrLf & Mid$(temp, 2): temp = ""
Next
End With
Open Range("A1").Value & Range("A2").Value For Output As #1
Print #1 , Mid$(txt, Len(vbCrLf) + 1)
Close #1
MsgBox ("Done - File has been created & saved")
End Sub


My question is the following:
Is it possible to have the data exported as 1 continuous data set? Like the following:
1,2,3,4,5,6,7,8,9,10,11,12,

If it is possible, what would the code look like to make this work?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Thank you Sumitpal...But I should have been more clear as I didn't write the original VBA code. Soooo...I actually would need to know the code (in it's completeness) if that's okay to ask for.
 
Upvote 0
Like this:

Code:
Sub test()
Dim i As Long, r As Range, temp$, txt$, rng As Range
Set rng = Range([a5], [a5].End(xlDown))
rng.NumberFormat = "General"
rng.HorizontalAlignment = xlLeft
Set rng = Range([b5], [b5].End(xlDown))
rng.NumberFormat = "General"
rng.HorizontalAlignment = xlLeft
Range([c5], [c5].End(xlDown)).NumberFormat = "General"
With Range("a5", Range("a" & Rows.Count).End(xlUp)).Resize(, 4)
    For i = 1 To .Rows.Count
        For Each r In .Rows(i).Cells
            temp = temp & "," & r.Text
        Next
        txt = txt & Mid$(temp, 2) & ","
        temp = ""
    Next
End With
Open [a1] & [a2] For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , txt
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
MsgBox ("Done - File has been created & saved")
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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