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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
There are no delimiters in the text file and it appears that there are no new line characters. You are dealing with a fixed length record and fixed length fields.
Do you have a document that describes the file format? And field lengths?
 
Upvote 0
There are no delimiters in the text file and it appears that there are no new line characters. You are dealing with a fixed length record and fixed length fields.
Do you have a document that describes the file format? And field lengths?
After working with the file you appear to have 150 character, fixed-length records. Here is a worksheet with all records imported, 3 columns of data.
I hope you can work with this.
The macro code to read the file is below: (change the file path and name if you have other files to import.)
VBA Code:
Option Explicit
Public Sub ImportData()
  Dim myRec As myRecord
  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

Link to Workbook with imported data
LEDG-01 Workbook
 
Upvote 0
I can't see why someone would need a 6,700 KB text file to test their solution on.
A couple hundred lines should be sufficient.
 
Upvote 0
After working with the file you appear to have 150 character, fixed-length records. Here is a worksheet with all records imported, 3 columns of data.
I hope you can work with this.
The macro code to read the file is below: (change the file path and name if you have other files to import.)
VBA Code:
Option Explicit
Public Sub ImportData()
  Dim myRec As myRecord
  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

Link to Workbook with imported data
LEDG-01 Workbook
sir can you guide me it is showing compile error user-defined type not defined which refrence i have to add in in tools-refrence
iam sorry asking you this but am not having much knowledge but trying
thanks in anticipattion
 

Attachments

  • error.PNG
    error.PNG
    49.8 KB · Views: 4
Upvote 0
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.
 
Upvote 0
thank you sir did follow your advice and went superrr fast however can you please help me and please guide me as i am having two problems
1) ResultArray(ArrayRow, 1) = Left(FileChunkString, ChunkFirstSpacePosition - 1) shows run time error 5 invalid procedure or call argument
2) if i want data to change in new row with change in word like "CRD" and data after that till next CRD what changes to be made

i am attaching code what i copied
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 = "d:\LEDG.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
Const Column_C_Data_Start As Long = 47 ' <--- Set this to the start position in the chunk for Column C data
Const LengthOfChunk As Long = 47 ' <--- Set this to the length for each chunk of data
Set ws = ThisWorkbook.Sheets("Sheet1")
' 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, 1) = Left(FileChunkString, ChunkFirstSpacePosition - 1)
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

please help
thanks
 
Upvote 0
sir can you guide me it is showing compile error user-defined type not defined which refrence i have to add in in tools-refrence
iam sorry asking you this but am not having much knowledge but trying
thanks in anticipattion
Delete that line, it is not needed
This is for the version of a import sub that I submitted.
 
Upvote 0
@james bond, Those changes that you made to the code that I submitted are causing the code to error. What were you trying to accomplish by changing the code:
VBA Code:
' 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
   Const Column_C_Data_Start   As Long = 47                                        ' <--- Set this to the start position in the chunk for Column C data
   Const LengthOfChunk         As Long = 47                                       ' <--- Set this to the length for each chunk of data
 
Upvote 0
@james bond, Those changes that you made to the code that I submitted are causing the code to error. What were you trying to accomplish by changing the code:
VBA Code:
' 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
   Const Column_C_Data_Start   As Long = 47                                        ' <--- Set this to the start position in the chunk for Column C data
   Const LengthOfChunk         As Long = 47                                       ' <--- Set this to the length for each chunk of data
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
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,103
Members
449,096
Latest member
provoking

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