fixed submain 🐥
' ===== MAIN MACRO =====
Sub MainMacro()
Dim inputData As String
' Step 1 — Get data from the selected text file
inputData = GetTextFromFile()
' Step 2 — Check if user canceled or file was empty
If inputData = "" Then
MsgBox "No data selected or file is empty.", vbExclamation
Exit Sub
End If
' Step 3 — Use the data (you can replace this logic)
MsgBox "Text file data loaded successfully!" & vbCrLf & vbCrLf & _
Left(inputData, 500) & vbCrLf & "..." & vbCrLf & "(Only first 500 characters shown)", vbInformation
' Step 4 — You can process or display the data below
' Example: Debug.Print inputData
' Or write it into cells, etc.
End Sub
' ===== HELPER FUNCTION =====
Function GetTextFromFile() As String
Dim filePath As String
Dim fileNo As Integer
Dim fileContent As String
Dim lineText As String
' Open File Picker Dialog
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select a Text File"
.Filters.Clear
.Filters.Add "Text Files", "*.txt"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Function ' Cancel pressed
filePath = .SelectedItems(1)
End With
' Read full content of file
fileNo = FreeFile
Open filePath For Input As #fileNo
Do Until EOF(fileNo)
Line Input #fileNo, lineText
fileContent = fileContent & lineText & vbCrLf
Loop
Close #fileNo
GetTextFromFile = fileContent
End Function
Comments
Post a Comment