當製作琴絃上的控制檯輸出對齊,我通常使用功能與空間填充(無論是向左或向右),當你想非常有用格式化輸出。
下面將適合任意數量的列來拆分您擁有的數組(更一般,因爲您可以設置要拆分的列數)。如果您只是想以表格的形式顯示數組,請使用TabulateArray(...)
。
Sub DisplayTuplesTest()
Dim arr1 As Variant, arr2 As Variant
arr1 = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
arr2 = Array(1.61, 3.22, 4.83, 6.44, 8.05, 9.66, 11.27, 12.88, 14.49, 16.1, 17.71, 19.32, 20.93, 22.54, 24.15, 25.76, 27.37, 28.98, 30.59, 32.2)
Call DisplayTuples(arr1, arr2, 2)
Call TabulateArray(arr2, 2)
End Sub
Sub DisplayTuples(Array1 As Variant, Array2 As Variant, iCols As Integer)
Dim Count1 As Integer, Count2 As Integer ' Counts # of items in the array
Dim Max1 As Integer, Max2 As Integer ' Tracks the max length of values
Dim n As Integer, oTmp As Variant ' Temporary variables
Dim r As Integer, c As Integer, rows As Integer, sTxt As String ' For displaying results
Count1 = UBound(Array1)
Count2 = UBound(Array2)
If Count1 <> Count2 Then
Debug.Print "Arrays are not same dimension!"
Exit Sub
End If
' Max length of Array1
Max1 = 0
For Each oTmp In Array1
n = Len(CStr(oTmp))
If Max1 < n Then Max1 = n
Next
' Max length of Array2
Max2 = 0
For Each oTmp In Array2
n = Len(CStr(oTmp))
If Max2 < n Then Max2 = n
Next
' Lets say we want 4 spaces from longest value of Array1 value before showing Array2 value
Max1 = Max1 + 4
' Lets say we want 8 spaces from longest value of Array2 value before showing Array1 value on next column
Max2 = Max2 + 8
' Rows required to display arrays into iCols columns
rows = Count1 \ iCols
If rows * iCols < Count1 Then rows = rows + 1 ' If an extra rows is required
' Display in a table like format, with spliting into iCols columns
For r = 0 To rows - 1
sTxt = ""
For c = 0 To iCols - 1
n = r + c * rows ' calculates the n'th index to display
If n > Count1 Then Exit For
sTxt = sTxt & SpacePadding_R(Array1(n), Max1) & SpacePadding_R(Array2(n), Max2)
Next
Debug.Print sTxt
Next
End Sub
Sub TabulateArray(Array1 As Variant, iCols As Integer)
Dim Count1 As Integer ' Counts # of items in the array
Dim Max1 As Integer, Max2 As Integer ' Tracks the max length of values
Dim n As Integer, oTmp As Variant ' Temporary variables
Dim r As Integer, c As Integer, rows As Integer, sTxt As String ' For displaying results
Count1 = UBound(Array1)
' Max length of n'th index
Max1 = Len(CStr(Count1 + 1))
' Lets say we want 3 spaces from max count of Array1 value before showing the Array1 value
Max1 = Max1 + 4
' Lets say we want 8 spaces from longest value of Array1 value before next column
Max2 = Max2 + 8
' Rows required to display arrays into iCols columns
rows = Count1 \ iCols
If rows * iCols < Count1 Then rows = rows + 1 ' If an extra rows is required
' Display in a table like format, with spliting into iCols columns
For r = 0 To rows - 1
sTxt = ""
For c = 0 To iCols - 1
n = r + c * rows ' calculates the n'th index to display
If n > Count1 Then Exit For
sTxt = sTxt & SpacePadding_R(n + 1, Max1) & SpacePadding_R(Array1(n), Max2)
Next
Debug.Print sTxt
Next
End Sub
Function SpacePadding_R(oValue As Variant, iMaxLen As Integer) As String
Dim sOutput As String, iLen As Integer
sOutput = CStr(oValue)
iLen = Len(sOutput)
If iLen < iMaxLen Then
' Pad spaces at the end of the input (Right side)
sOutput = sOutput & String(iMaxLen - iLen, " ")
Else
' Optional if you want to restrict this
sOutput = Left(sOutput, iMaxLen)
End If
SpacePadding_R = sOutput
End Function
輸出與樣本陣列(高亮向您顯示添加空格):
'vba'或'vb.net'?這些是不同的框架。你能展示你期望的結果嗎? – Fabio
已更新的問題@Fabio – vzhen
使用'vbTab'常量:'Console.WriteLine(「{0} {1} {2}」,column1Value,vbTab,column2Value)' – Fabio