so what I need to do is fill an array using an InputBox, then but that in an array, sort it alphabetically and then output it to the current word document. I have it almost most complete, the issue is it is only outputting the last word to the document. Im guessing my loop is wrong but I cannot find VBA documentation to do this to save my life. Thank you
Option Explicit
This is the main sub that declares the array
Sub Main()
Dim ListArr() As String
ListArr = Get_Input_List()
Call Bubble_Sort_Ascending(ListArr)
Call Output_List_To_Document(ListArr)
End Sub
Function to get input and fill array
Function Get_Input_List() As String()
Dim list As String
list = InputBox("Please enter words to sort separated with a comma and no spaces", "Words")
Get_Input_List = Split(list, ",")
End Function
Sorts the array alphabetically
Sub Bubble_Sort_Ascending(listNewArray() As String)
Dim SrtTemp As Variant
Dim inputWord As Variant
Dim i As Long
Dim j As Long
'Alphabetize Sheet Names in Array List
For i = LBound(listNewArray) To UBound(listNewArray)
For j = i To UBound(listNewArray)
If listNewArray(i) > listNewArray(j) Then
SrtTemp = listNewArray(j)
listNewArray(j) = listNewArray(i)
listNewArray(i) = SrtTemp
End If
Next j
Next i
End Sub
This is the problem, I cannot output the whole array to the word document. I have found plenty of documentation on how to do this into an excel spreadsheet but almost nothing for word.
Sub Output_List_To_Document(newListArray() As String)
Dim inputWord As Variant
Dim i As Long
Dim j As Long
For i = LBound(newListArray) To UBound(newListArray)
For j = i To UBound(newListArray)
For Each inputWord In newListArray
ActiveDocument.Range = inputWord & vbCrLf
Next
Next j
Next i
End Sub
Output_List_To_Document
has three nested loops. Is this intentional? Your innerFor Each
should be sufficient. Also, consider using theJoin
function, withvbCrLf
as a delimiter to build a complete string, and then you can append the entire array in one go. – Zev Spitz