2

因此,我在 vba 中执行了一项相对简单的任务,但对结果摸不着头脑。我编写了一个列标题数组,然后尝试通过 Application.WorksheetFunction.Transpose 函数粘贴它们,但没有运气......它只是将数组中的第一个值粘贴到范围的长度。

这是我的代码:

<i>  sub test
     Dim myHeadings() As Variant
     Dim header_rng As Range

     myHeadings = Array("a", "b" , "C", "D")

     'Set header_rng = Range(Cells(10, 2), Cells(10, UBound(myHeadings)))

     Dim name As String
     Dim name_Array As String
     name = TixCollection(1).ADR

     Call PrintArray(10, 2, myHeadings, name)


     End sub


     Sub PrintArray(RowPrint, ColPrint, ArrayName, WorkSheetName)

    'Dim element As Variant
    'For Each element In ArrayName
    'Debug.Print element
    'Next element


    Sheets(WorkSheetName).Range(Cells(RowPrint, ColPrint), _
    Cells(RowPrint, UBound(ArrayName))) = _
    WorksheetFunction.Transpose(ArrayName)

    End Sub

这有什么问题?

4

1 回答 1

0

如评论中所述,如果您希望将标题放置在不同列的同一行上,则不需要转置数组。但是,您需要将列数添加到起始列(在您的情况下为 2)。这是我在下面工作的代码。

Sub PasteHeadersToSheet()

    Dim myHeadings() As Variant
    myHeadings = Array("a", "b", "C", "D")

    ' set sheet name
    sheetName = "sheet1"

    ' set starting row
    startRow = 10

    ' set starting row
    startColumn = 2

    Call PrintArray(startRow, startColumn, myHeadings, sheetName)


End Sub


Sub PrintArray(RowPrint, ColPrint, ArrayName, WorkSheetName)

    ' set worksheet
    Set ws = Sheets(WorkSheetName)

    ' set starting cell
    Set cellStart = Cells(RowPrint, ColPrint)

    ' set ending cell
    Set cellEnd = Cells(RowPrint, UBound(ArrayName) + ColPrint)

    ' paste array to selected worksheet and range of cells
    ws.Range(cellStart, cellEnd) = ArrayName

End Sub
于 2014-12-18T21:29:56.240 回答