在vba中动态填充和打印2D数组

时间:2018-06-25 14:14:18

标签: arrays vba search multidimensional-array

我正在遍历2D数组,以查找工作表上是否已存在第1列的值。如果是这样,我想将第2列中的值粘贴到找到的同一列中。我的代码在下面...我没有收到任何错误,但是没有任何值正在打印。

我要粘贴的范围必须是动态的,现在基于标题为“ myArrayNoBlanks”的数组(动态数组,该数组填充要提取的原始值)。也许将范围更改为可以找到最后一列的内容可以解决问题?

我们将不胜感激。

For h = 1 To 11

Set PartRange = Worksheets(12).UsedRange.Columns(col(h))
Set CostRange = Worksheets(12).UsedRange.Columns(Cost(h))

    For Each cell In PartRange.Cells
        If cell.Row > 1 Then
        ReDim Preserve myParts(var)
        myParts(var) = cell.Value
        var = var + 1
        End If
    Next cell

    For Each cell In CostRange.Cells
        If cell.Row > 1 Then
        ReDim Preserve myCost(varc)
        myCost(varc) = cell.Value
        varc = varc + 1
        End If
    Next cell

    myNewArray = Array(myParts, myCost)

    For w = LBound(myNewArray, 1) To UBound(myNewArray, 1)

        Dim cols As Integer
        Dim ans As Boolean
        Dim sch As Long
        sch = myNewArray(w)(2)
        Dim ch As Range
        Dim rngs As Range
        Set rngs = Range("B1")
        Set rngs = rngs.Resize(1, UBound(myArrayNoBlanks))
        For Each ch In rngs
            If ch = sch Then
                ans = True
                cols = ch.Column
                    If h = 1 Then
                    Dim Dest1 As Range
                    Set Dest1 = Worksheets(14).Cells(2, cols)
                    Dest1.Value = sch
                    End If
            End If
        Next ch
        If IsNull(ans) Then ans = False


    Next w

0 个答案:

没有答案