send data to a text file





Send data to a .txt file

Data mining and data capture processes rely on being able to work effectively whilst still extracting the relevant data. This short code below uses the print #1 function to grab specific text or parts of text and put them directly into a ‘closed’ text file on any given connected drive or network location.

Private Sub Send_data_to_a_.txt_file
Dim FileUser As String
Dim FileuserDocFILE As String
FileUser = ActiveWorkbook.Name
'On Error Resume Next
FileuserDocFILE = "C:\My Documents\Send_to_text.txt" 'name the .txt database to hold the info
'On Error GoTo 0
'This section lets you choose the fields to go into the .txt file
'The Chr(9) allows a Tab to be included between the fields
Open FileuserDocFILE For Append As #1 'Use Output instead of APPEND to overwrite data
Print #1, Worksheets("Data").Range("A1") & Chr(9) & _
Close #1
End If
End Sub

This specific code will write the contents of cell A1 into the text file, then a tab before adding the contents of cell B1 alongside. Using a loop, or script to run through a large amount of data is efficiently handled by this code, although some adaptation to the code will need to be made and each time it runs the new data will appear underneath the last value added. Contact us for a fully bespoke short script under our data mining service.