如何正确地分配一个不是从索引1开始到Excel范围的数组范围?

时间:2019-02-09 11:09:38

标签: arrays excel vba

当我执行下面显示的代码时,数组会自动将自身重新分配为Z(1 to 10),并且我不喜欢它如何重新分配维度,因为我一直想保持数组位置与其中的行位置相同电子表格。但我仍然可以通过将它们彼此相等,将数组分配给excel范围,如下所示:

enter image description here

Dim range_test As Range
Set range_test = ThisWorkbook.Worksheets("Sheet1").Range("A2:A11")

ReDim Z(2 To 11) As Variant
Z = range_test
ThisWorkbook.Worksheets("Sheet1").Range("D2:D11") = Z

但是当我包括整个范围(包括第一行或标题)时,我的问题是我无法使用上面使用的代码将数组分配给excel范围,否则它将包含标题。所以我会必须按照以下所示方式进行操作:

Dim range_test As Range
Set range_test = ThisWorkbook.Worksheets("Sheet1").Range("A1:A11")

ReDim Z(1 To 11) As Variant
Z = range_test

Dim i As Long
For i = 2 To 11
    ThisWorkbook.Worksheets("Sheet1").Range("E" & i) = Z(i, 1)
Next i

我想做的事情与逻辑类似,在该逻辑中,数组使您可以像其他语言(例如MATLAB)一样对其进行切片,因为与将数组等同于数组相比,创建循环填充数组的资源更加繁重范围,反之亦然(如果我错了,请纠正我)。请参考以下概念:

ThisWorkbook.Worksheets("Sheet1").Range("G2:G11") = Z(2 to 11)
ThisWorkbook.Worksheets("Sheet1").Range("H2:H11") = Y(2 to 11)

2 个答案:

答案 0 :(得分:2)

如果我对您的理解正确,则可以避免出现这种情况

ThisWorkbook.Worksheets("Sheet1").Range("E2:E11").Resize(range_test.Count - 1) = Application.Index(Z, Evaluate("row(2:10)"))

您无需对“ 2:10”位进行硬编码。

enter image description here

答案 1 :(得分:1)

大于1的数组LBound

标题,偏移,调整大小

  • 示例1和2涉及Range("A2:A11")
  • 示例3和4分别涉及Range("A1:A11")

代码

Option Explicit

Sub Array1()
    Dim range_test As Range
    Set range_test = ThisWorkbook.Worksheets("Sheet1").Range("A2:A11")

    ReDim Z(2 To 11) As Variant
    'ReDim Z(range_test.Row, range_test.Row + range_test.Rows.Count - 1)
    Z = range_test
    ThisWorkbook.Worksheets("Sheet1").Range("D2:D11") = Z
End Sub

Sub Array2()
    Dim range_test As Range
    Dim Z As Variant
    Set range_test = ThisWorkbook.Worksheets("Sheet1").Range("A2:A11")

    Z = range_test
    ThisWorkbook.Worksheets("Sheet1").Range("D2:D11") = Z
End Sub

Sub Array3()
    Const HeaderRows As Long = 1
    Dim range_test As Range
    Set range_test = ThisWorkbook.Worksheets("Sheet1").Range("A1:A11")

    ReDim Z(HeaderRows + 1 To 11) As Variant
    'ReDim Z(HeaderRows + range_test.Row, _
            range_test.Row + range_test.Rows.Count - 1)
    Z = range_test.Offset(HeaderRows).Resize(range_test.Rows.Count - HeaderRows)
    ThisWorkbook.Worksheets("Sheet1").Range("D2:D11") = Z
End Sub

Sub Array4()
    Const HeaderRows As Long = 1
    Dim range_test As Range
    Dim Z As Variant
    Set range_test = ThisWorkbook.Worksheets("Sheet1").Range("A1:A11")

    Z = range_test.Offset(HeaderRows).Resize(range_test.Rows.Count - HeaderRows)
    ThisWorkbook.Worksheets("Sheet1").Range("D2:D11") = Z
End Sub

分步结论

Sub Array5()

    Const cSheet As Variant = "Sheet1"  ' Worksheet Name/Index
    Const cHeaders As Long = 1          ' Number of Header Rows
    Const cSource As String = "A1:A11"  ' Initial Range Address
    Const cTarget As Variant = "D"      ' Target Column Letter/Number

    Dim rngInit As Range      ' Initial Range
    Dim rngSource As Range    ' Source Range
    Dim rngFirst As Range     ' First Cell Range of Target Range
    Dim rngTarget As Range    ' Target Range
    Dim vntSource As Variant  ' Source Array

    With ThisWorkbook.Worksheets(cSheet)
        ' Create a reference to Initial Range.
        Set rngInit = .Range(cSource)
        With rngInit
            ' Calculate Source Range by manipulating Initial Range.
            Set rngSource = .Offset(cHeaders).Resize(.Rows.Count - cHeaders)
        End With
        ' Copy Source Range to Source Array.
        vntSource = rngSource
        ' Calculate First Cell Range of Target Range.
        Set rngFirst = .Cells(rngInit.Row + cHeaders, cTarget)
        ' Adjust size of Target Range to size of Source Array.
        Set rngTarget = rngFirst.Resize(UBound(vntSource))
        ' Write Source Array to Target Range.
        rngTarget = vntSource
    End With

End Sub

最终结论(无对象引用)

Sub Array6()

    Const cSheet As Variant = "Sheet1"  ' Worksheet Name/Index
    Const cHeaders As Long = 1          ' Number of Header Rows
    Const cSource As String = "A1:A11"  ' Initial Range Address
    Const cTarget As Variant = "D"      ' Target Column Letter/Number

    Dim vntSource As Variant  ' Source Array

    With ThisWorkbook.Worksheets(cSheet).Range(cSource)
        ' Calculate Source Range by manipulating Initial Range.
        ' Copy Source Range to Source Array.
        vntSource = .Offset(cHeaders).Resize(.Rows.Count - cHeaders)
        ' Calculate First Cell Range of Target Range.
        ' Adjust size of Target Range to size of Source Array.
        ' Write Source Array to Target Range.
        .Parent.Cells(.Row + cHeaders, cTarget) _
               .Resize(UBound(vntSource)) = vntSource
    End With

End Sub

备注

我不知道你可以做

  • Redim,之前Dim下没有Option Explicit
  • Dim一个1D数组并将其范围复制到“转换” 放入2D数组中。