daviduk001
New Member
- Joined
- Jan 25, 2021
- Messages
- 9
- Office Version
- 365
- Platform
- Windows
I have created VBA import multiple text files into the same sheet and put each file text into one cell
but I found a problem with excel limited to 32,767 characters in one cell.
Then I split files when characters are over the limit and put it into the next row.
Now I have another issue there is the problem is some phrase is split.
I don't want to split the word, I want to split its end of the line of the phrase before over the max character.
Does anyone help with this?
if you required big text files to test please go here and you can download text files
and this is my current code
Dim i As Integer
but I found a problem with excel limited to 32,767 characters in one cell.
Then I split files when characters are over the limit and put it into the next row.
Now I have another issue there is the problem is some phrase is split.
I don't want to split the word, I want to split its end of the line of the phrase before over the max character.
Does anyone help with this?
if you required big text files to test please go here and you can download text files
and this is my current code
Dim i As Integer
VBA Code:
Sub ImportTextFiles()
i = 2
SelectMultipleFiles
MsgBox ("Execution Completed...!")
End Sub
Function SelectMultipleFiles()
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = True
.Title = "Please select the files"
.Filters.Clear
.Filters.Add "All supported files", "*.txt"
.Filters.Add "Text Files", "*.txt"
If .Show = True Then
Dim fPath As Variant
For Each fPath In .SelectedItems
ImportFile fPath
Next
End If
End With
End Function
Function ImportFile(Path)
Dim my_file As Integer
Dim file_name As String
Dim allText As String
Dim fso As New Scripting.FileSystemObject
file_name = Path
'my_file = FreeFile()
Open file_name For Binary As #1
allText = Space$(LOF(1))
Get #1, , allText
Close #1
Dim allTxtArr
allTxtArr = SplitString(allText, 32700)
Dim item As Variant
Dim fileNameWithoutExt As String
Dim idx As Integer
fileNameWithoutExt = fso.GetBaseName(file_name)
idx = 1
For Each item In allTxtArr
item = Trim(item)
If (item <> Empty) Then
Cells(i, 1).Value = fileNameWithoutExt & idx
Cells(i, 2).Value = Trim(item)
i = i + 1
idx = idx + 1
End If
Next
allText = ""
allTxtArr = Null
End Function
Public Function SplitString(ByVal str As String, ByVal numOfChar As Long) As String()
Dim sArr() As String
Dim nCount As Long
ReDim sArr(Len(str) \ numOfChar)
Do While Len(str)
sArr(nCount) = Left$(str, numOfChar)
str = Mid$(str, numOfChar + 1)
nCount = nCount + 1
Loop
SplitString = sArr
End Function