1
所以我需要做的是使用InputBox填充數組,然後在數組中按字母順序對其進行排序,然後將其輸出到當前的Word文檔。我有它幾乎最完整的,問題是它只是輸出文件的最後一個字。我猜我的循環是錯誤的,但我找不到VBA文檔來保存我的生活。謝謝VBA輸出數組內容到Word文檔
Option Explicit
這是聲明數組
Sub Main()
Dim ListArr() As String
ListArr = Get_Input_List()
Call Bubble_Sort_Ascending(ListArr)
Call Output_List_To_Document(ListArr)
End Sub
功能以獲取輸入和填充陣列
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
排序排列的字母順序
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
主要子這是問題,我c annot將整個數組輸出到word文檔。我發現了很多關於如何在excel電子表格中執行此操作的文檔,但幾乎沒有任何字。
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'有三個嵌套循環。這是故意的嗎?你的內在'For Each'應該足夠了。另外,考慮使用'Join'函數,'vbCrLf'作爲分隔符來構建完整的字符串,然後您可以一次追加整個數組。 –