VBA Import text file to excel with desired result

roykana

Active Member
Joined
Mar 8, 2018
Messages
311
Office Version
  1. 2010
Platform
  1. Windows
Dear All Master,
I want to import from text file to excel and want the desired result as below and for column H I mark yellow then the result must be text.
I also attached a text file link
thanks
roykana

DESIRED RESULT
VBA IMPORT TEXT FILE.xlsm
ABCDEFGHIJKLMNOPQR
1No Transaction Date Dept.Code Pel.Name Customer Address No. Cd. ItemName Item QtyUnit Price Pot. % Total Pot. : Tax :Costs : Total End :
20002/KSR/TK/122119-12-21 GENERAL GENERAL 1410288TAMAKA R 410288 RC/L-TOP 1PCS 11500001150004000000800000
30002/KSR/TK/122119-12-21 GENERAL GENERAL 2829740TAMAKA R 829740 RC/L-TOP 2PCS 900000180000
40002/KSR/TK/122119-12-21 GENERAL GENERAL 3410240TAMAKA R 410240 RC/L-TOP 1PCS 1250000125000
50002/KSR/TK/122119-12-21 GENERAL GENERAL 456117ALFIN TRAVEL 56117 D1680 TG 1PCS 1050000105000
60002/KSR/TK/122119-12-21 GENERAL GENERAL 5222445TAMAKA R 222445 RC/L-TOP/USB 1PCS 1550000155000
70002/KSR/TK/122119-12-21 GENERAL GENERAL 6111195TAMAKA R 111195 RC/L-TOP/USB 1PCS 1600000160000
80003/KSR/TK/122119-12-21GENERAL GENERAL 12019ALFIN WB 2019 BATIK 1PCS 3500003500000035000
MASTER



VBA Code:
Option Explicit

Sub Importtextfile()
    '// Declare a variable as
    Dim nRow            As Long
    Dim sExtension      As String
    Dim oFolder         As FileDialog '// FileDialog object
    Dim vSelectedItem   As Variant
    Dim wsSelect        As Worksheet
    '// Stop Screen Flickering
    Application.ScreenUpdating = False

    '// Create a FileDialog object as a File Picker dialog box
    Set oFolder = Application.FileDialog(msoFileDialogOpen)
    Set wsSelect = Sheets("MASTER")

    '// Use a With...End With block to reference FileDialog.
    With oFolder
        '// Allow multiple selection.
        .AllowMultiSelect = True
        '// Use the Show method to display the files.
        If .Show = -1 Then

    '// Extension
    sExtension = Dir("*.txt")

    '// Step through each SelectedItems
    For Each vSelectedItem In .SelectedItems

        '// Sets Row Number for Data to Begin
        nRow = Range("A1").End(xlUp).Offset(1, 0).Row
With wsSelect.Range("A1").CurrentRegion.Clear
End With
        '// Below is importing a text file
        With wsSelect.QueryTables.Add(Connection:= _
            "TEXT;" & sExtension, Destination:=Range("$A$" & nRow))
            .Name = sExtension
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = ""
            .TextFileTrailingMinusNumbers = False
            .Refresh BackgroundQuery:=False
        End With
        sExtension = Dir
    Next
            '// If Cancel...
            Else
            End If
    End With

    Application.ScreenUpdating = True

    '// Set object to Nothing. Object? see Link Object
    Set oFolder = Nothing
End Sub
link text file
 

Attachments

  • screenshot text file.JPG
    screenshot text file.JPG
    101.3 KB · Views: 34

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
try changing
VBA Code:
With wsSelect.Range("A1").CurrentRegion.Clear
End With
to
VBA Code:
With wsSelect
    .Range("A1").CurrentRegion.Clear
    .Columns("H").Cells.NumberFormat = "@"
End With
 
Upvote 0
I want to import from text file to excel and want the desired result as below and for column H I mark yellow then the result must be text.
Just a bad import setting as it should be directly achieved within the QueryTables.Add block​
like any beginner can operate just activating the Macro Recorder and well answering to the Import Wizard Assistant​
in particular here for each column format …​
 
Upvote 0
try changing
VBA Code:
With wsSelect.Range("A1").CurrentRegion.Clear
End With
to
VBA Code:
With wsSelect
    .Range("A1").CurrentRegion.Clear
    .Columns("H").Cells.NumberFormat = "@"
End With
@NoSparks
thanks for your reply. But here what I mean is import from textfile to excel so you can see the link of the sample text file. so my import results are still messy or not in accordance with the results I want like the results from excel that I posted in this post
Thanks
Roykana
 
Upvote 0
Just a bad import setting as it should be directly achieved within the QueryTables.Add block​
like any beginner can operate just activating the Macro Recorder and well answering to the Import Wizard Assistant​
in particular here for each column format …​
@Marc L

thanks for your reply. But here what I mean is import from textfile to excel so you can see the link of the sample text file. so my import results are still messy or not in accordance with the results I want like the results from excel that I posted in this post. It's not like what you said if you find it easy please make an excel output like me from the text file that I share
Thanks
Roykana
 
Upvote 0
so you can see the link of the sample text file
Yes but, in order to waste time like in your previous thread, according to your linked text file​
I need your filled result macro workbook as well with a link like for your text file​
so I could choose to improve your way (so easy as any beginner can operate manually just well answering to the Import Wizard Assistant)​
or share a direct way (should look like pretty the same than in your previous thread).​
I hope also to have some revert in your previous thread before to go deeper here …​
 
Last edited:
Upvote 0
Correction of previous post : « in order to avoid to waste time … »​
 
Upvote 0
You must paste this VBA demonstration to the MASTER worksheet module :​
VBA Code:
Sub Demo1()
  Const N = "No. *"
    Dim V, F%, H, K, S, W, L&, T, R&, X
        ChDrive ThisWorkbook.Path:  ChDir ThisWorkbook.Path
    With Application
        V = .GetOpenFilename("Report files,*.txt", , "Select a file"):  If V = False Then Exit Sub
        F = FreeFile
        H = [{1,1;2,3;4,6;5,7}]
        K = [{7,1;8,2;9,3;10,8;11,9;12,10;13,11;14,12}]
        UsedRange.Offset(1).Clear
       .ScreenUpdating = False
       .ThousandsSeparator = "."
       .UseSystemSeparators = False
        Open V For Input As #F
        S = Split(Input(LOF(F), #F), vbCrLf)
        Close #F
        ReDim V(UBound(S), 1 To 18)
        W = .Match(N, S, 0)
    While IsNumeric(W)
        L = .Match("Pot. : *", S, 0) - 1
        T = .Trim(Split(S(L), vbTab))
        S(L) = "":  S(W - 1) = ""
        For L = 1 To 4:  V(R, L + 14) = T(2 + (L - 1) * 3):  Next
        T = .Trim(Split(S(W - 2), vbTab, 8))
    Do
        X = .Trim(Split(S(W), vbTab)):  If Not IsNumeric(X(1)) Then Exit Do
        For L = 1 To UBound(H):  V(R, H(L, 1)) = T(H(L, 2)):  Next
        For L = 1 To UBound(K):  V(R, K(L, 1)) = X(K(L, 2)):  Next
        R = R + 1
        W = W + 1
    Loop
        W = .Match(N, S, 0)
    Wend
    If R Then
        If .International(32) <> 1 Then
            For R = 0 To R - 1
                X = Split(V(R, 2), "/"):  If UBound(X) = 2 Then V(R, 2) = X(2) & "/" & X(1) & "/" & X(0)
            Next
        End If
        With [A2].Resize(R, UBound(V, 2))
            .Columns(8).NumberFormat = "@"
            .FormulaLocal = V
        End With
    End If
       .UseSystemSeparators = True
       .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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