Search code examples
vbaexcel-2007

How to import tab delimited data into Excel using VBA


Given tab delimited data that I am importing from Excel, how do I insert it into the spreadsheet so that it ends up in multiple cells like it does when I paste it.

Edit: I have the text in a variable, so I don't want to go through the file system.


Solution

  • This is basically what I finally came up with, it was a little more complex and used arrays, but that is the gist of it.

    i = 1
    For Each Row In VBA.Split(text, vbCrLf)
        j = 1
        For Each Col In VBA.Split(Row, vbTab)
            ActiveSheet.Cells(i, j).Value = Col
            j = j + 1
        Next Col
        i = i + 1
    Next Row