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
I should not comment it if you are happy with it (even if you use useless things)…​
According to your post #13 where the table already exists - so no need to create it ! - the combination of my post #6 VBA procedure​
with my post #16 well works on my side …​
@Marc L
yes it is true that before the table already exists then I eliminate the table and also I add columns so that's why I use the method of creating the table. But what's important to me is that the code you give you goes perfectly.
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
As the table already exists you just need to resize it rather than eliminate it and recreate it …​
 
Upvote 0
As the table already exists you just need to resize it rather than eliminate it and recreate it …​
@Marc L
okay I follow your advice and from the code I posted #19 please you can fix it so that it becomes the end result of the solution code and also importantly I can in the future I can add columns not to be difficult
 
Upvote 0

First clear the data range combining UsedRange & Offset or just via the ListObject DataBodyRange property​
then add the data whatever rows and / or columns then just resize the table according to the whole range …​
 
Upvote 0
First clear the data range combining UsedRange & Offset or just via the ListObject DataBodyRange property​
then add the data whatever rows and / or columns then just resize the table according to the whole range …​
@Marc L
Running perfectly as you recommend.
You are indeed very best and
I have the latest maybe you can help me .

thanks
roykana
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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