Copy all text between numbers from a text file to Excel sheet rows.

sr1111

New Member
Joined
Sep 2, 2022
Messages
46
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
Copy all text between numbers from a text file to Excel sheet rows.
attached:
Text.txt (Input) shown as image

Excel file (Output)

key1.xlsx
A
1
21. How to copy text in key:1# rows betwen numbers from a text file. How to copy text in rows betwen numbers from a text file. How to copy text in rows betwen numbers from a text file. How to copy text in rows betwen numbers from a text file.
33. How to copy text in rows betwen numbers from a key:1# text file. How to copy text in rows betwen numbers from a text file.
45. How to copy key:1# text in rows betwen numbers from a text file . How to copy text in rows betwen numbers from a text file 8090. How to copy text in rows betwen numbers from a text file How to copy text in rows betwen numbers from a text file 8090. How to copy text in rows betwen numbers from a text file How to copy text in rows betwen numbers from a text file 8090. How to copy text in rows betwen numbers from a text file 8090. How to copy text in rows betwen numbers from a text file
5100. How to copy text key:1# in rows betwen numbers from a text file August 10 . How to copy text in rows betwen numbers from a text file
6200. key:1# 20. How to copy text in rows betwen numbers from a text file How to copy text in rows betwen numbers from a text file
Sheet1
 

Attachments

  • Screenshot 12-09-2022 073229.png
    Screenshot 12-09-2022 073229.png
    23 KB · Views: 17

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi.

Let me know if this works for you. To test it, simply put all of this code in it's own standard VBA module, change the filePath and sheetName to what they need to be in the top sub, and then run the top sub. It starts putting data in row 2 in column A, but as you can see, you can change both the column letter and the start row number too. (I used regular expressions.)
VBA Code:
Option Explicit

Sub Copy_Text_Between_Numbers_In_Text_File_To_Worksheet()
Dim s1$, s2$, filePath$, sheetName$, starts() As String, i&, columnLetter$, rowNumber&

columnLetter = "A"
rowNumber = 2
filePath = "C:\Users\Chris\Desktop\Sample File.txt"
sheetName = ActiveSheet.Name

s1 = Put_All_Data_In_A_Text_File_Into_A_String(filePath)
s2 = s1 'Passing s1 in the following function modifies it, so we make a copy.
starts = Split(List_Of_Start_Locations(s1, "[0-9]+[.]"), ",")

Application.Calculation = xlCalculationManual
For i = 0 To UBound(starts) - 1
    Sheets(sheetName).Range(columnLetter & i + rowNumber).Value = SubString(s2, CLng(starts(i)), CLng(starts(i + 1) - 1))
Next i
Sheets(sheetName).Range(columnLetter & i + rowNumber).Value = SubString(s2, CLng(starts(i)), Len(s2))
Application.Calculation = xlCalculationAutomatic

End Sub
Function List_Of_Start_Locations(strValue$, strPattern$) As String
Dim str1$, counter%, i&, finish&, previousFinish&, location&
counter = 0: previousFinish = 0
str1 = strValue: List_Of_Start_Locations = "1"
With CreateObject("VBScript.RegExp")
    .Pattern = strPattern
    .IgnoreCase = True
start:
    If .test(strValue) = True Then
        counter = counter + 1
        location = .Execute(strValue)(0).firstindex + 1
        strValue = .Replace(strValue, "ß")
        strValue = SubString(strValue, InStr(strValue, "ß") + 1, Len(strValue))
        If counter > 1 Then
            finish = previousFinish + location
            i = 1
            Do While Consists_Of_Only_Numberical_Digits(SubString(str1, finish + i, finish + i)) = False
                i = i + 1
            Loop
            finish = finish + i - 1
            List_Of_Start_Locations = List_Of_Start_Locations & "," & finish + 1
            previousFinish = finish
        End If
        GoTo start
    End If
End With

End Function
Function Consists_Of_Only_Numberical_Digits(strValue As String) As Boolean
Consists_Of_Only_Numberical_Digits = strValue Like WorksheetFunction.Rept("[0-9]", Len(strValue))
End Function
Function Put_All_Data_In_A_Text_File_Into_A_String(filePath As String)
With CreateObject("Scripting.FileSystemObject").OpenTextFile(filePath)
    Put_All_Data_In_A_Text_File_Into_A_String = .ReadAll
    .Close
End With
End Function
Function SubString(str As String, start As Long, finish As Long)
On Error Resume Next
SubString = Mid(str, start, finish - start + 1)
End Function
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Hi.

Let me know if this works for you. To test it, simply put all of this code in it's own standard VBA module, change the filePath and sheetName to what they need to be in the top sub, and then run the top sub. It starts putting data in row 2 in column A, but as you can see, you can change both the column letter and the start row number too. (I used regular expressions.)
VBA Code:
Option Explicit

Sub Copy_Text_Between_Numbers_In_Text_File_To_Worksheet()
Dim s1$, s2$, filePath$, sheetName$, starts() As String, i&, columnLetter$, rowNumber&

columnLetter = "A"
rowNumber = 2
filePath = "C:\Users\Chris\Desktop\Sample File.txt"
sheetName = ActiveSheet.Name

s1 = Put_All_Data_In_A_Text_File_Into_A_String(filePath)
s2 = s1 'Passing s1 in the following function modifies it, so we make a copy.
starts = Split(List_Of_Start_Locations(s1, "[0-9]+[.]"), ",")

Application.Calculation = xlCalculationManual
For i = 0 To UBound(starts) - 1
    Sheets(sheetName).Range(columnLetter & i + rowNumber).Value = SubString(s2, CLng(starts(i)), CLng(starts(i + 1) - 1))
Next i
Sheets(sheetName).Range(columnLetter & i + rowNumber).Value = SubString(s2, CLng(starts(i)), Len(s2))
Application.Calculation = xlCalculationAutomatic

End Sub
Function List_Of_Start_Locations(strValue$, strPattern$) As String
Dim str1$, counter%, i&, finish&, previousFinish&, location&
counter = 0: previousFinish = 0
str1 = strValue: List_Of_Start_Locations = "1"
With CreateObject("VBScript.RegExp")
    .Pattern = strPattern
    .IgnoreCase = True
start:
    If .test(strValue) = True Then
        counter = counter + 1
        location = .Execute(strValue)(0).firstindex + 1
        strValue = .Replace(strValue, "ß")
        strValue = SubString(strValue, InStr(strValue, "ß") + 1, Len(strValue))
        If counter > 1 Then
            finish = previousFinish + location
            i = 1
            Do While Consists_Of_Only_Numberical_Digits(SubString(str1, finish + i, finish + i)) = False
                i = i + 1
            Loop
            finish = finish + i - 1
            List_Of_Start_Locations = List_Of_Start_Locations & "," & finish + 1
            previousFinish = finish
        End If
        GoTo start
    End If
End With

End Function
Function Consists_Of_Only_Numberical_Digits(strValue As String) As Boolean
Consists_Of_Only_Numberical_Digits = strValue Like WorksheetFunction.Rept("[0-9]", Len(strValue))
End Function
Function Put_All_Data_In_A_Text_File_Into_A_String(filePath As String)
With CreateObject("Scripting.FileSystemObject").OpenTextFile(filePath)
    Put_All_Data_In_A_Text_File_Into_A_String = .ReadAll
    .Close
End With
End Function
Function SubString(str As String, start As Long, finish As Long)
On Error Resume Next
SubString = Mid(str, start, finish - start + 1)
End Function
Nope the VBA has not given the result as shown in the "Excel file (Output)" above.
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

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