text to columns - delimited

Forestq

Active Member
Joined
May 9, 2010
Messages
482
Hi,

I have csv file. Normally in excel I select column "A" and can do -> text to column -> delimited >semicolon and everything will be done ok. I need to do it exaclty this same but in VBa.

Code:
sub TEST
Dim FileToOpen, sourceFile, outputFile As String
Dim TextLine() As Integer
Dim sourceWb As Workbook

FileToOpen = OpenFile("XLS Files", "*.xls", ThisWorkbook.Path & "\DATA\")
 
    If FileToOpen <> "" Then
            Set sourceWb = GetObject(FileToOpen)
                         
              
     
    End If
End sub

Function Open, but I can also use
Code:
 Application.Dialogs(xlDialogOpen).Show
Code:
Function OpenFile(extFilterName As String, extFilter As String, nameFilter As String) As String
On Error GoTo ErrH
Dim fDialog As Office.FileDialog

Set fDialog = Application.FileDialog(msoFileDialogOpen)
With fDialog
    .Filters.Clear
    .Filters.Add extFilterName, extFilter
    .InitialFileName = nameFilter
End With
fDialog.Show
OpenFile = fDialog.SelectedItems.Item(1)
Exit Function
ErrH:
    OpenFile = ""
End Function
 
This is how I handle csv imports....

Code:
Sub ImportHandler()

Dim FileToOpen As String
Workbooks.Add
FileToOpen = Application.GetOpenFilename(MultiSelect:=False)
    If Len(FileToOpen) = 0 Then
        MsgBox "No file specified.", vbExclamation, "Error"
        Exit Sub
    Else

    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & FileToOpen, Destination:=Range("A1"))
        .Name = "Export Test"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(4, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

End If

End Sub

You'd need to change the delimiter setup, but it should do the job for you.. (also, you may want to amend the .textfilecolumndatatypes to your liking)
 
Upvote 0

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