submain macro
Sub MainMacro()
Dim bulkData As String
Dim lines() As String
Dim i As Long
' Get large input text from the user
bulkData = GetLargeInput("Paste your bulk data below:")
' Split data by line breaks
lines = Split(bulkData, vbCrLf)
' Clear existing data on the active sheet
Cells.Clear
' Insert each line into a new row in column A
For i = LBound(lines) To UBound(lines)
Cells(i + 1, 1).Value = lines(i)
Next i
MsgBox "All data has been successfully added to the sheet!" & vbCrLf & _
"Total lines: " & (UBound(lines) - LBound(lines) + 1), vbInformation
End Sub
'---------------------------------------
' Function to get large input via Notepad
'---------------------------------------
Function GetLargeInput(prompt As String) As String
Dim tempFile As String
Dim fileNum As Integer
Dim userInput As String
' Create a temporary text file in the user's temp folder
tempFile = Environ("TEMP") & "\LargeInput.txt"
' Open Notepad with the temp file
Shell "notepad.exe " & tempFile, vbNormalFocus
MsgBox prompt & vbCrLf & vbCrLf & _
"Paste your text in Notepad, save (Ctrl+S), close Notepad, then click OK here.", vbInformation
' Wait until user closes Notepad
Do While FileLocked(tempFile)
DoEvents
Loop
' Read the file content
fileNum = FreeFile
Open tempFile For Input As #fileNum
userInput = Input$(LOF(fileNum), #fileNum)
Close #fileNum
' Delete the temp file
Kill tempFile
' Return the data
GetLargeInput = userInput
End Function
'---------------------------------------
' Helper function to check if a file is locked
'---------------------------------------
Function FileLocked(filename As String) As Boolean
On Error Resume Next
Dim fileNum As Integer
fileNum = FreeFile
Open filename For Binary Access Read Write Lock Read Write As #fileNum
Close fileNum
If Err.Number <> 0 Then
FileLocked = True
Err.Clear
Else
FileLocked = False
End If
On Error GoTo 0
End Function
Comments
Post a Comment