如何打印拆分列?

时间:2015-07-12 20:23:36

标签: vba

假设开始为1且停止为10.我想在列编号中打印1到5,然后在另一列编号中打印6 - 10.

我可以在单循环中执行此操作吗?或者我必须在另一个循环中做? 另外,我无法弄清楚如何从{1, -10}

的第二列开始
    Console.WriteLine("{0,-10}", "Number")

    Dim split = (starting + stopping) / 2

    For A = starting To split
        Console.WriteLine("{0,-10}", A)
    Next

更新 - 预期结果

Number Number
1      6
2      7
3      8
4      9
5      10

更新2 - 让它们对齐 我们不是使用Space(),而是使用任何其他解决方案,我们可以将它们设置为相同的宽度并将它们对齐在中 enter image description here

2 个答案:

答案 0 :(得分:1)

我会用一个循环来写这样的东西:

Sub TwoColumns(ByVal starting as Integer,ByVal stopping as Integer)
  Dim split As Integer,i As Integer
  split = (starting + stopping) / 2
  For i=starting  to split
    Debug.Print (i) & Space(5) &  (i+split-starting)
  Next i
End Sub

答案 1 :(得分:0)

当在控制台输出上使字符串对齐时,我通常使用带有空间填充的函数(向左或向右),当你想要格式化输出时非常有用。

下面将适合任意数量的列来拆分您拥有的数组(更通用,因为您可以设置要拆分的列数)。如果您只想在表格中显示数组,请使用 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

使用样本数组输出(突出显示以显示添加的空格):
Sample Output