简单的工作表循环并将单元格值放入vba数组

时间:2016-06-22 14:22:04

标签: arrays excel vba excel-vba

我想做一个非常简单的程序,但由于某种原因它不起作用。 我想循环遍历工作簿中的每个工作表并获取O13的单元格值并将其放入数组中。然后在1个工作表中打印整个数组。出于某种原因,它没有读入数字。我尝试将数组设置为variant,integer和double,没有任何效果。

     Dim WS_Count As Integer

     Dim I As Integer

     WS_Count = ActiveWorkbook.Worksheets.Count

     Dim array1(43) As Double

     For I = 1 To WS_Count

        array1(I) = ActiveWorkbook.Worksheets(I).Cells(13, 15).Value

     Next I

     Range("A40") = array1

3 个答案:

答案 0 :(得分:2)

您需要在1 Dim array1(1 to 43) As Double中启动数组,然后设置范围以放置数组并使用Transpose Range("A40:A83") = Application.Transpose(array1)

答案 1 :(得分:1)

这适用于我:

Public Sub SWL()

    Dim WS_Count    As Long
    Dim I           As Long
    Dim array1(43)  As String
    Dim str         As String

    WS_Count = ActiveWorkbook.Worksheets.Count

    For I = 1 To WS_Count

       array1(I) = ActiveWorkbook.Worksheets(I).Cells(1, 1).Value

    Next I

    str = CStr(Join(array1, " "))
    ActiveWorkbook.Worksheets(1).Cells(1, 2) = str

End Sub

答案 2 :(得分:0)

使用WorksheetFunction.Transpose方法将数组转置到您希望输出的范围内。

Range("A40:A83") = WorksheetFunction.Transpose(array1)