+ Reply to Thread
Results 1 to 4 of 4

VBA to convert .txt to .xlsx

  1. #1
    Registered User
    Join Date
    12-14-2020
    Location
    Milan, Italy
    MS-Off Ver
    Microsoft 365
    Posts
    29

    VBA to convert .txt to .xlsx

    Hi All,

    I am trying to create a very simple user form which does converts multiple .txt to .xlsx.
    The problem i am having is when i try to convert the file i got this error: "Compile error: ByRef argument type mismatch".

    Any ideas how to sole the issue?


    The user form contains only 2 buttons.
    Code:

    Dim filesToConvert As Collection
    Private Sub ToggleButton1_Click()

    Dim fileDialog As fileDialog
    Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
    fileDialog.AllowMultiSelect = True
    fileDialog.Title = "Select .txt file to convert"
    If fileDialog.Show = -1 Then
    For Each file In fileDialog.SelectedItems
    Next
    End If

    End Sub
    Private Sub ToggleButton2_Click()
    For Each file In filesToConvert
    ConvertTxtToXls file
    Next file
    Set filesToConvert = New Collection
    MsgBox "Conversion completed!", vbInformation, [OK!]
    Sub ConvertTxtToXls(txtFilePath As String)
    Workbooks.OpenText txtFilePath, , , xlDelimited, , , True
    ActiveWorkbook.SaveAs Replace(txtFilePath, ".txt", ".xlsx"), xlOpenXMLWorkbook
    ActiveWorkbook.Close

    End Sub

    Private Sub UserForm_Click()
    Set filesToConvert = New Collection
    End Sub

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,322

    Re: VBA to convert .txt to .xlsx

    You're also missing an End Sub, but your current problem is that file is a variant and your sub expects a string. Use:

    Please Login or Register  to view this content.
    or pass the argument ByVal
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    12-14-2020
    Location
    Milan, Italy
    MS-Off Ver
    Microsoft 365
    Posts
    29

    Re: VBA to convert .txt to .xlsx

    Thanks for the reply, Rorya.

    Could you please tell me where is the end sub? In fact now i am getting this error saying me that there is missing sub end.

    Thx

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,322

    Re: VBA to convert .txt to .xlsx

    After the Msgbox line

+ 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. Convert .xlsx to .csv
    By Mayasak in forum Excel General
    Replies: 2
    Last Post: 08-18-2023, 07:57 PM
  2. [SOLVED] convert xlsx to txt (VBA)
    By fairyme in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-02-2023, 04:30 AM
  3. Help with code to convert xls to xlsx and continue with xlsx file
    By 3345james in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-02-2016, 11:47 AM
  4. how to convert .xlsx to .csv
    By Nehrav in forum Excel General
    Replies: 4
    Last Post: 11-04-2013, 07:24 AM
  5. [SOLVED] Won't convert xlsx to pdf well
    By sockerplaya37 in forum Excel General
    Replies: 2
    Last Post: 06-23-2013, 10:17 PM
  6. Convert xlsx to dat
    By swmmr1928 in forum Excel General
    Replies: 5
    Last Post: 03-13-2013, 05:30 PM

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