İmport txt files in excel with VBA codes

sy43fb

New Member
Joined
Jan 18, 2020
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
I wanna import txt files in excel with VBA. I want to import text one by one column.
İf in text file
abcd 123 x+-
32 7 k 698755 then import like below
A B C D E F G H I J K L M N
1 a b c d 1 2 3 x + -
2 3 2 7 k 6 9 8 7 6 5 5

I need to codes. Can anybody help me?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I make macro record it seems so

Sub Makro1()
'
' Makro1 Makro
'

'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;D:\2019-2020 Mat deneme takip\abc.TXT", Destination:=Range("$A$1"))
.CommandType = 0
.Name = "abc"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 932
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 17, 22, 4, 15, 10, 2, 2, 5, _
3, 12, 10, 2, 3, 2, 3, 7, 4, 3, 2, 7, 13, 21, 7, 5, 11)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

How can I edit for 300 column.
İt seems very difficult to make for 300 columns.
 
Upvote 0
If your text files are very large, you may want to load them into a variant array and then dump the array into a properly dimensioned range.
Chances are though that this will do well enough to get you started.

VBA Code:
Sub CallFromHere()
    Application.ScreenUpdating = False
    ImportSomeTextCharByChar [a1], Application.GetOpenFilename() 'where [a1] = top/left cell
    Application.ScreenUpdating = True
End Sub

Sub ImportSomeTextCharByChar(r As Range, fileName As String)
    Dim s() As String, x As Long, y As Long, l As Long, c() As String
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(fileName)
        s() = Split(.ReadAll, vbCrLf)
        .Close
    End With
   
    For x = 0 To UBound(s)
        s(x) = Replace(s(x), " ", "")
        l = Len(s(x)) - 1
        If l > -1 Then
            ReDim c(0, l)
            For y = 0 To l
                c(0, y) = Mid(s(x), y + 1, 1)
            Next
            Range(r.Offset(x), r.Offset(x, l)) = c()
        End If
    Next
End Sub
 
Upvote 0
Thanks for your response. İt seems good but I have a problem.
Null characters not import in this way.
12 3 import in excel 12 3
A3 and A4 is empty
 
Upvote 0
I don't understand and I have no way of knowing what characters are in your file. A null character to me is chr(0). I don't know what character is between "12" and "3".
Add this line of code and see if it fixes it.

VBA Code:
Sub CallFromHere()
    Application.ScreenUpdating = False
    ImportSomeTextCharByChar [a1], Application.GetOpenFilename() 'where [a1] = top/left cell
    Application.ScreenUpdating = True
End Sub

Sub ImportSomeTextCharByChar(r As Range, fileName As String)
    Dim s() As String, x As Long, y As Long, l As Long, c() As String
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(fileName)
        s() = Split(.ReadAll, vbCrLf)
        .Close
    End With
   
    For x = 0 To UBound(s)
        s(x) = WorksheetFunction.Clean(s(x))
        s(x) = Replace(s(x), " ", "")
        l = Len(s(x)) - 1
        If l > -1 Then
            ReDim c(0, l)
            For y = 0 To l
                c(0, y) = Mid(s(x), y + 1, 1)
            Next
            Range(r.Offset(x), r.Offset(x, l)) = c()
        End If
    Next
End Sub

see: s(x) = WorksheetFunction.Clean(s(x))
 
Upvote 0
Thanks for codes.
s(x) = WorksheetFunction.Clean(s(x))
syntax error
but I can't find it.
 
Upvote 0
It's in my second post about 1/2 way down in the code example. The function should remove troubling characters. :)
 
Upvote 0
thanks for your interest.
I attached pictures.
your codes result is page1.gif
I wanna make like page2.gif.
txtscreen like txtscreen.gif
Thanks again. Sorry for bothering you.
 

Attachments

  • page1.gif
    page1.gif
    247.9 KB · Views: 6
  • page2.gif
    page2.gif
    149.4 KB · Views: 6
  • txtscreen.gif
    txtscreen.gif
    195.7 KB · Views: 6
Upvote 0
abcd 123 x+-
32 7 k 698755 then import like below
A B C D E F G H I J K L M N
1 a b c d 1 2 3 x + -
2 3 2 7 k 6 9 8 7 6 5 5

I'm just following your instructions! :)
Your initial post led me to believe that we were gonna ignore spaces.
So remove the following line of code:
VBA Code:
s(x) = Replace(s(x), " ", "")
Let me know if that works for you.
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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