convert file which opens in 1 row in text to multiple columns in excel

james bond

New Member
Joined
Aug 25, 2023
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
attached is link to file which opens in txt but when i import to excel comes in 1 row and column and shows not imported fully
can anyone please explain how i can import it as shown in the table
please someone said by power query but i really need to understand step by step as i really dont know and would be indebtited

thanking you in anticipation regards

PQ Import TXT One Row Pivot AliGW (2).xlsx
ABC
2CRD0120001010197051000001/010197/CASHSALES0
3CRD0120001010197053000001/010197/CASHSALES0
4CRD0120001010197052000001/010197/CASHSALES0
5CRD0120001010197054000001/010197/CASHSALES0
6CRD0110001010197035000001/010197/CASHSALES0
7CRD0120002020197051000002/020197/CASHSALES0
8CRD0120002020197053000002/020197/CASHSALES0
9CRD0120002020197054000002/020197/CASHSALES0
10CRD0120002020197052000002/020197/CASHSALES0
11CRD0110002020197035000002/020197/CASHSALES0
12CRD0120003030197051000003/030197/CASHSALES0
13CRD0120003030197053000003/030197/CASHSALES0
14CRD0120003030197054000003/030197/CASHSALES0
15CRD0120003030197052000003/030197/CASHSALES0
16CRD0110003030197035000003/030197/CASHSALES0
17CRD0120004040197051000004/040197/CASHSALES0
18CRD0120004040197053000004/040197/CASHSALES0
19CRD0120004040197054000004/040197/CASHSALES0
20CRD0120004040197052000004/040197/CASHSALES0
21CRD0110004040197035000004/040197/CASHSALES0
22CRD012000505019705100INVOICECANCELLED0
23CRD012000505019705300INVOICECANCELLED0
24CRD012000505019705400INVOICECANCELLED0
25CRD012000505019705200INVOICECANCELLED0
26CRD011000505019703500INVOICECANCELLED0
27CSL0120501270197057000501/270197/CASHSALES5401-5500234000
28CSL0120501270197057000501/270197/CASHSALES5401-550019500
29CSL0120501270197057000501/270197/CASHSALES5401-550064500
30CSL0120501270197057000501/270197/CASHSALES5401-5500302995
31CSL0110501270197006000501/270197/CASHSALES5401-5500102349
32CSL0110501270197035000501/270197/CASHSALES5401-5500921146
33CSL0120502310197057000502/310197/CASHSALES7701-771370000
34CSL0120502310197057000502/310197/CASHSALES7701-771323500
35CSL0120502310197057000502/310197/CASHSALES7701-771321500
36CSL0120502310197057000502/310197/CASHSALES7701-771331000
37CSL0110502310197006000502/310197/CASHSALES7701-77137300
38CSL0110502310197035000502/310197/CASHSALES7701-7713138700
39CSL0220001010297051000001/010297/CASHSALES26247440
40CSL0220001010297053000001/010297/CASHSALES4146352
41CSL0220001010297054000001/010297/CASHSALES3080682
42CSL0210001010297035000001/010297/CASHSALES33474474
43CSL0220002020297051000002/020297/CASHSALES25239646
44CSL0220002020297053000002/020297/CASHSALES4972239
45CSL0220002020297054000002/020297/CASHSALES3999690
46CSL0210002020297035000002/020297/CASHSALES34211575
47CSL0220003030297051000003/030297/CASHSALES17162177
48CSL0220003030297053000003/030297/CASHSALES4031008
49CSL0220003030297054000003/030297/CASHSALES2046780
50CSL0220003030297052000003/030297/CASHSALES91424
51CSL0210003030297035000003/030297/CASHSALES23331389
52CSL0220004040297051000004/040297/CASHSALES19337181
53CSL0220004040297053000004/040297/CASHSALES6019034
54CSL0220004040297054000004/040297/CASHSALES2387520
1
 
Here is something to try that works fast:

VBA Code:
Sub ImportTextData()
'
    Dim ArrayRow                As Long
    Dim ChunkFirstSpacePosition As Long
    Dim FileStringLength        As Long
    Dim FileChunkString         As String, FileString   As String
    Dim ResultArray()           As Variant
    Dim ws                      As Worksheet
'
    Const FilePath              As String = "C:\Users\Laptop\Downloads\LEDG-01.txt" ' <--- Set this to the location of your text file
    Const Column_C_Data_Start   As Long = 97                                        ' <--- Set this to the start position in the chunk for Column C data
    Const LengthOfChunk         As Long = 150                                       ' <--- Set this to the length for each chunk of data
    Set ws = Sheets("1")                                                            ' <--- Set this to the name of the worksheet to displaay the results into
'
    Open FilePath For Input As #1                                                   ' Open the text file
    FileString = Input$(LOF(1), 1)                                                  ' Read the contents of the file into FileString
    Close #1                                                                        ' Close the ext file
'
    ArrayRow = 1                                                                    ' Initialize ArrayRow
'
    FileStringLength = Len(FileString)                                              ' Get the length of data in the text file
'
    ReDim ResultArray(1 To FileStringLength / 150, 1 To 3)                          ' Establish the dimensions of the ResultArray
'
    Do While (ArrayRow - 1) * LengthOfChunk < FileStringLength                      ' Loop through the chunks of data from the text file
        FileChunkString = Mid(FileString, (ArrayRow - 1) * _
                LengthOfChunk + 1, LengthOfChunk)                                   '   Get the chunk of data to scrape data from
'
        ChunkFirstSpacePosition = InStr(1, FileChunkString, " ")                    '   Find the position of the first blank in the FileChunkString
'
        ResultArray(ArrayRow, 1) = Left(FileChunkString, ChunkFirstSpacePosition - 1) '   Save the data for column A into ResultArray
        ResultArray(ArrayRow, 2) = Trim(Mid(FileChunkString, ChunkFirstSpacePosition + 1, _
                Column_C_Data_Start - ChunkFirstSpacePosition - 1))                 '   Save the data for column B into ResultArray
        ResultArray(ArrayRow, 3) = Mid(FileChunkString, Column_C_Data_Start, _
                LengthOfChunk - Column_C_Data_Start)                                '   Save the data for column C into ResultArray
'
        ArrayRow = ArrayRow + 1                                                     '   Increment ArrayRow
    Loop                                                                            ' Loop back
'
    ws.Range("A2").Resize(UBound(ResultArray, 1), UBound(ResultArray, 2)) = ResultArray ' Display the results to the result sheet
'
    ws.UsedRange.EntireColumn.AutoFit                                               ' Autofit the width of the columns
End Sub

You will have to check the results to see if it works for you.
sir also ResultArray(ArrayRow, 1) = Left(FileChunkString, ChunkFirstSpacePosition - 1) shows subscript out of range
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
sorry sir just have written before also if you could guide me as to how instead of number of characters if you can tell me how to add in new row everytime it starts with CRD
i am extremely sorry if i made a mistake
Not all the records start with CRD.
You will have to base your record length on 150 characters.
 
Upvote 0
Not all the records start with CRD.
You will have to base your record length on 150 characters.
Here are working versions of both the original code that I sent you and a modified version of the code submitted by james bond.
You will need to modify the filepath and the sheet name where data will be stored.
the code james bond submitted runs much faster because it reads the entire file into memory, where the other version reads and writes the text file line by line.
There are more than 6000 records in the source file.

There are also approximately 15 different record types (i.e. the first 3 characters: CRD, CLO, COM, ...)

Bosquedeguate Code
VBA Code:
Option Explicit
Public Sub ImportData()
  Dim str
  Dim r As Long
  Dim rng As Range
  Open "C:/users/Forrest/Documents/Projects/MrExcel/LEDG-01.TXT" For Input As #1
  r = 1
  Do While Not EOF(1)
    str = Input(150, #1)
    With myRec
      Set rng = ActiveSheet.Range("A" & r & ":H" & r)
      rng(1, 1) = Left(str, 21)
      rng(1, 2) = Mid(str, 22, 75)
      rng(1, 3) = Val(Mid(str, 97, 12))
      rng(1, 4) = Mid(str, 109, 42)
    End With
    r = r + 1
  Loop
  Close #1
End Sub


James Bond code
VBA Code:
Option Explicit
Sub ImportTextData()
'
    Dim ArrayRow                As Long
    Dim ChunkFirstSpacePosition As Long
    Dim FileStringLength        As Long
    Dim FileChunkString         As String, FileString   As String
    Dim ResultArray()           As Variant
    Dim ws                      As Worksheet
'
    Const FilePath              As String = "C:\Users\forrest\Documents\Projects\MrExcel\LEDG-01.txt" ' <--- Set this to the location of your text file
    Const Column_C_Data_Start   As Long = 97                                        ' <--- Set this to the start position in the chunk for Column C data
    Const LengthOfChunk         As Long = 150                                       ' <--- Set this to the length for each chunk of data
    Set ws = Sheets("Sheet2")                                                       ' <--- Set this to the name of the worksheet to displaay the results into
'
    Open FilePath For Input As #1                                                   ' Open the text file
    FileString = Input$(LOF(1), 1)                                                  ' Read the contents of the file into FileString
    Close #1                                                                        ' Close the ext file
'
    ArrayRow = 1                                                                    ' Initialize ArrayRow
'
    FileStringLength = Len(FileString)                                              ' Get the length of data in the text file
'
    ReDim ResultArray(1 To FileStringLength / 150, 1 To 4)                          ' Establish the dimensions of the ResultArray
'
    Do While (ArrayRow - 1) * LengthOfChunk < FileStringLength                      ' Loop through the chunks of data from the text file
        FileChunkString = Mid(FileString, (ArrayRow - 1) * _
                LengthOfChunk + 1, LengthOfChunk)                                   '   Get the chunk of data to scrape data from
'
        ChunkFirstSpacePosition = InStr(1, FileChunkString, " ")                    '   Find the position of the first blank in the FileChunkString
'
        ResultArray(ArrayRow, 1) = Left(FileChunkString, 21) '   Save the data for column A into ResultArray
        ResultArray(ArrayRow, 2) = Trim(Mid(FileChunkString, 22, 75))
        ResultArray(ArrayRow, 3) = Trim(Mid(FileChunkString, 97, 12))
        ResultArray(ArrayRow, 4) = Trim(Mid(FileChunkString, 109, 42))
'
        ArrayRow = ArrayRow + 1                                                     '   Increment ArrayRow
    Loop                                                                            ' Loop back
'
    ws.Range("A2").Resize(UBound(ResultArray, 1), UBound(ResultArray, 2)) = ResultArray ' Display the results to the result sheet
'
    ws.UsedRange.EntireColumn.AutoFit                                               ' Autofit the width of the columns
End Sub
 
Upvote 0
Here are working versions of both the original code that I sent you and a modified version of the code submitted by james bond.
You will need to modify the filepath and the sheet name where data will be stored.
the code james bond submitted runs much faster because it reads the entire file into memory, where the other version reads and writes the text file line by line.
There are more than 6000 records in the source file.

There are also approximately 15 different record types (i.e. the first 3 characters: CRD, CLO, COM, ...)

Bosquedeguate Code
VBA Code:
Option Explicit
Public Sub ImportData()
  Dim str
  Dim r As Long
  Dim rng As Range
  Open "C:/users/Forrest/Documents/Projects/MrExcel/LEDG-01.TXT" For Input As #1
  r = 1
  Do While Not EOF(1)
    str = Input(150, #1)
    With myRec
      Set rng = ActiveSheet.Range("A" & r & ":H" & r)
      rng(1, 1) = Left(str, 21)
      rng(1, 2) = Mid(str, 22, 75)
      rng(1, 3) = Val(Mid(str, 97, 12))
      rng(1, 4) = Mid(str, 109, 42)
    End With
    r = r + 1
  Loop
  Close #1
End Sub


James Bond code
VBA Code:
Option Explicit
Sub ImportTextData()
'
    Dim ArrayRow                As Long
    Dim ChunkFirstSpacePosition As Long
    Dim FileStringLength        As Long
    Dim FileChunkString         As String, FileString   As String
    Dim ResultArray()           As Variant
    Dim ws                      As Worksheet
'
    Const FilePath              As String = "C:\Users\forrest\Documents\Projects\MrExcel\LEDG-01.txt" ' <--- Set this to the location of your text file
    Const Column_C_Data_Start   As Long = 97                                        ' <--- Set this to the start position in the chunk for Column C data
    Const LengthOfChunk         As Long = 150                                       ' <--- Set this to the length for each chunk of data
    Set ws = Sheets("Sheet2")                                                       ' <--- Set this to the name of the worksheet to displaay the results into
'
    Open FilePath For Input As #1                                                   ' Open the text file
    FileString = Input$(LOF(1), 1)                                                  ' Read the contents of the file into FileString
    Close #1                                                                        ' Close the ext file
'
    ArrayRow = 1                                                                    ' Initialize ArrayRow
'
    FileStringLength = Len(FileString)                                              ' Get the length of data in the text file
'
    ReDim ResultArray(1 To FileStringLength / 150, 1 To 4)                          ' Establish the dimensions of the ResultArray
'
    Do While (ArrayRow - 1) * LengthOfChunk < FileStringLength                      ' Loop through the chunks of data from the text file
        FileChunkString = Mid(FileString, (ArrayRow - 1) * _
                LengthOfChunk + 1, LengthOfChunk)                                   '   Get the chunk of data to scrape data from
'
        ChunkFirstSpacePosition = InStr(1, FileChunkString, " ")                    '   Find the position of the first blank in the FileChunkString
'
        ResultArray(ArrayRow, 1) = Left(FileChunkString, 21) '   Save the data for column A into ResultArray
        ResultArray(ArrayRow, 2) = Trim(Mid(FileChunkString, 22, 75))
        ResultArray(ArrayRow, 3) = Trim(Mid(FileChunkString, 97, 12))
        ResultArray(ArrayRow, 4) = Trim(Mid(FileChunkString, 109, 42))
'
        ArrayRow = ArrayRow + 1                                                     '   Increment ArrayRow
    Loop                                                                            ' Loop back
'
    ws.Range("A2").Resize(UBound(ResultArray, 1), UBound(ResultArray, 2)) = ResultArray ' Display the results to the result sheet
'
    ws.UsedRange.EntireColumn.AutoFit                                               ' Autofit the width of the columns
End Sub
very very obliged a big thanks for helping me out
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,043
Members
449,092
Latest member
ikke

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