o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
create a vab loop all txt file from a folder
- User
create a vab loop all txt file from a folder
- Assistant
create a vab loop all txt file from a folder
Sure! Below is an example of a VBA macro that loops through all .txt files in a specified folder. This example is for Excel VBA, but the code can be adapted for Word or other VBA environments.
.txt files in a folder.Sub LoopThroughTxtFiles()
Dim FSO As Object
Dim folderPath As String
Dim file As Object
Dim folder As Object
Dim fileDialog As FileDialog
Dim textLine As String
Dim row As Long
' Prompt user to select a folder
Set fileDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fileDialog
.Title = "Select Folder Containing TXT Files"
.AllowMultiSelect = False
If .Show = -1 Then ' If the user clicks "OK"
folderPath = .SelectedItems(1)
Else ' If the user clicks "Cancel"
MsgBox "No folder selected.", vbExclamation
Exit Sub
End If
End With
' Create FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
Set folder = FSO.GetFolder(folderPath)
' Initialize row counter for Excel worksheet
row = 1
' Loop through each file in the folder
For Each file In folder.Files
' Check if the file has a .txt extension
If LCase(FSO.GetExtensionName(file.Name)) = "txt" Then
' Open and read the text file
Dim textStream As Object
Set textStream = FSO.OpenTextFile(file.Path, 1) ' 1 = ForReading
' Read the file content line by line
Do While Not textStream.AtEndOfStream
textLine = textStream.ReadLine
' Write the content to the active worksheet
Cells(row, 1).Value = textLine
row = row + 1
Loop
' Close the text stream
textStream.Close
End If
Next file
MsgBox "Processing complete!", vbInformation
End Sub
ALT + F11 to open the Visual Basic for Applications (VBA) editor.Insert > Module.F5 or go back to Excel, and run the macro from Developer > Macros..txt files..txt files are processed.Cells(row, 1).Value to specify a different worksheet or cell range.FileSystemObject, so there's no need to set additional references.1 = ForReading2 = ForWriting8 = ForAppending' Inside the If block
If LCase(FSO.GetExtensionName(file.Name)) = "txt" Then
' Open and read the text file
Dim textStream As Object
Set textStream = FSO.OpenTextFile(file.Path, 1) ' 1 = ForReading
' Read only the first line
If Not textStream.AtEndOfStream Then
textLine = textStream.ReadLine
Cells(row, 1).Value = textLine
row = row + 1
End If
' Close the text stream
textStream.Close
End If
' Add a counter variable
Dim lineCount As Long
' Inside the If block
If LCase(FSO.GetExtensionName(file.Name)) = "txt" Then
' Initialize line counter
lineCount = 0
' Open and read the text file
Dim textStream As Object
Set textStream = FSO.OpenTextFile(file.Path, 1) ' 1 = ForReading
' Count the lines
Do While Not textStream.AtEndOfStream
textStream.ReadLine
lineCount = lineCount + 1
Loop
' Write the file name and line count to the worksheet
Cells(row, 1).Value = file.Name
Cells(row, 2).Value = lineCount
row = row + 1
' Close the text stream
textStream.Close
End If
.txt files..txt and not hidden (e.g., .txt with a space).File > Options > Trust Center > Trust Center Settings > Macro Settings.Let me know if you need further assistance or modifications to this code!
Not yet included in a published release.