+ Reply to Thread
Results 1 to 4 of 4

Need vba code: How to export 2 column sheet to textfile / import textfile to 2 columns

Hybrid View

frankytheman Need vba code: How to export... 01-10-2020, 07:45 AM
WideBoyDixon Re: Need vba code: How to... 01-10-2020, 08:14 AM
frankytheman Re: Need vba code: How to... 01-10-2020, 09:16 AM
WideBoyDixon Re: Need vba code: How to... 01-10-2020, 09:24 AM
  1. #1
    Registered User
    Join Date
    12-26-2019
    Location
    Germany
    MS-Off Ver
    2016
    Posts
    69

    Need vba code: How to export 2 column sheet to textfile / import textfile to 2 columns

    Hi everyone,

    I need your help.
    I have a worksheet called Contacts.
    Column A is Contact Name
    Column B is Contact Number
    I would like to do a backup of the 2 columns and export it into a text file.
    I would like to do a restore, import the textfile into the 2 columns.
    Here is the code I currently have, it exports it into a textfile but when I import it back, it writes everyting in column A.
    Also attached the actual workbook.

    Thank you in advance.

    '<---CONTACTS BACKUP--->
    Private Sub CommandButton1_Click()
    Dim rng As Range, lRow As Long
    Dim stOutput As String, stNextLine As String, stSeparator As String
    Dim stFilename As String, stEncoding As String
    Dim fso As Object
    
    
    Set rng = Worksheets("Contacts").UsedRange
    stFilename = "C:\Users\" & Environ("Username") & "\Desktop\BackUp.txt"
    stSeparator = vbTab 
    stEncoding = "UTF-8"
    
    For lRow = 1 To rng.Rows.Count
        If rng.Columns.Count = 1 Then
            stNextLine = rng.Rows(lRow).Value
        Else
            stNextLine = Join$(Application.Transpose(Application.Transpose(rng.Rows(lRow).Value)), stSeparator)
        End If
        If stOutput = "" Then
            stOutput = stNextLine
        Else
            stOutput = stOutput & vbCrLf & stNextLine
        End If
    Next lRow
    
    Set fso = CreateObject("ADODB.Stream")
    With fso
        .Type = 2
        .Charset = stEncoding
        .Open
        .WriteText stOutput
        .SaveToFile stFilename, 2
    End With
    Set fso = Nothing
    End Sub
    
    
    '<---CONTACTS RESTORE--->
    Private Sub CommandButton2_Click()
    Dim MyInputFile As String
    Dim TempFileNum As Integer
    Dim LineData As String
    MyInputFile = "C:\Users\" & Environ("Username") & "\Desktop\BackUp.txt"
     
    TempFileNum = FreeFile
        
    RowNumber = 1
     
    Open MyInputFile For Input As #TempFileNum
     
    Do While Not EOF(TempFileNum)
    
    Line Input #TempFileNum, LineData
     
    
    Worksheets("Contacts").Range("A" & RowNumber).Value = LineData
     
    RowNumber = RowNumber + 1
    Loop
     
    Close #TempFileNum
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Need vba code: How to export 2 column sheet to textfile / import textfile to 2 columns

    If you're going to write it with ADODB.Stream then perhaps you should read it using the same. See below:

    '<---CONTACTS BACKUP--->
    Private Sub CommandButton1_Click()
    
    Dim rng As Range, lRow As Long
    Dim stOutput As String, stNextLine As String, stSeparator As String
    Dim stFilename As String, stEncoding As String
    Dim fso As Object
    
    Set rng = Worksheets("Contacts").UsedRange
    stFilename = "C:\Users\" & Environ("Username") & "\Desktop\BackUp.txt"
    stSeparator = vbTab
    stEncoding = "UTF-8"
    
    For lRow = 1 To rng.Rows.Count
        If rng.Columns.Count = 1 Then
            stNextLine = rng.Rows(lRow).Value
        Else
            stNextLine = Join$(Application.Transpose(Application.Transpose(rng.Rows(lRow).Value)), stSeparator)
        End If
        If stOutput = "" Then
            stOutput = stNextLine
        Else
            stOutput = stOutput & vbCrLf & stNextLine
        End If
    Next lRow
    
    Set fso = CreateObject("ADODB.Stream")
    With fso
        .Type = 2
        .Charset = stEncoding
        .Open
        .WriteText stOutput
        .SaveToFile stFilename, 2
        .Close
    End With
    Set fso = Nothing
    
    MsgBox "Contact list saved!"
    Unload Me
    
    End Sub
    '<---CONTACTS RESTORE--->
    Private Sub CommandButton2_Click()
    Dim MyInputFile As String
    Dim FileData As String
    Dim LineData As Variant
    Dim LineCols As Variant
    Dim fso As Object
    Dim lRow As Long
    
    MyInputFile = "C:\Users\" & Environ("Username") & "\Desktop\BackUp.txt"
    
    Set fso = CreateObject("ADODB.Stream")
    With fso
        .Type = adTypeText
        .Charset = "UTF-8"
        .Open
        .LoadFromFile MyInputFile
        FileData = .ReadText
        .Close
    End With
    Set fso = Nothing
    
    LineData = Split(FileData, vbCrLf)
    For lRow = 0 To UBound(LineData)
        LineCols = Split(LineData(lRow), vbTab)
        Worksheets("Contacts").Range("A" & lRow + 1).Resize(1, UBound(LineCols) + 1).Value = LineCols
    Next lRow
    
    MsgBox "Contact list restored!"
    
    Unload Me
    
    End Sub
    Note I made a small change to the Backup routine to close the stream. I mostly re-wrote the restore routine.

    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Registered User
    Join Date
    12-26-2019
    Location
    Germany
    MS-Off Ver
    2016
    Posts
    69

    Re: Need vba code: How to export 2 column sheet to textfile / import textfile to 2 columns

    Sweet, I got an error however in the Restore routine at: .Type = adTypeText, I changed it to .Type = 2 and it works like magic now.
    Thanks a lot, it's exactly what I was looking for. You are a star!

  4. #4
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Need vba code: How to export 2 column sheet to textfile / import textfile to 2 columns

    Quote Originally Posted by frankytheman View Post
    Sweet, I got an error however in the Restore routine at: .Type = adTypeText, I changed it to .Type = 2 and it works like magic now.
    Thanks a lot, it's exactly what I was looking for. You are a star!
    Oops. Yes. I had a reference to the ADODB library in my code - sorry about that. Glad it's working for you

    WBD

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Export/import between Excel and textfile
    By Axmed.cm in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-13-2017, 05:38 PM
  2. Implementing TextFile import Delimiter settings on TXT Import VBA Script
    By Canuckle777 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-19-2013, 02:06 PM
  3. Macros code to Import textfile to existing sheet
    By luckygrass711 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-08-2012, 10:51 PM
  4. Import textfile to excel
    By Petter120 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-09-2012, 05:13 PM
  5. [SOLVED] Import textfile to excel and split (spaces) to column !
    By Petter120 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-09-2012, 02:04 PM
  6. Pick textfile in combobox and read last entry in textfile to sheet!
    By Petter120 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-09-2012, 07:11 AM
  7. Export as Textfile with VBA
    By moondark in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-14-2005, 09:45 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1