得到数组的整行 [英] get entire row of array

查看:140
本文介绍了得到数组的整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code以下,

我要得到整个行不只是原始数组的第1列,如何将我做到这一点?

 子例1()
    昏暗的arrValues​​()为Variant
    昏暗LASTROW只要
    昏暗filteredArray()
    昏暗lRow只要
    昏暗lCount只要
    昏暗tempArray()    LASTROW =表(原始数据)。UsedRange.Rows(表(原始数据)。UsedRange.Rows.Count).Row
    arrValues​​ =表(原始数据)。范围(单元格(2,1),电池(LASTROW,21))。值    先用临时数组只有一维
    使用ReDim tempArray(1至UBound函数(arrValues​​))
    对于lCount = 1到UBound函数(arrValues​​)
        如果arrValues​​(lCount,3)=手机之后
            lRow = lRow + 1
            tempArray(lRow)= arrValues​​(lCount,1)
        万一
    下一个    现在我们知道了filteredArray多大需要是:找到的值复制到该
    使用ReDim filteredArray(1至lRow,1比1)
    对于lCount = 1到lRow
        filteredArray(lCount,1)= tempArray(lCount)
    下一个    。表(L)范围(A2:U&放大器; 1 + lRow)= filteredArray
结束小组


解决方案

借助使用ReDim声明可以添加使用记录在 preSERVE 参数,但只有到最后的排名上即时。这是因为通常认为是列,而第一类是行两个标注尺寸数组的第二级的问题。

Application.Transpose 可翻转行转换成列,反之亦然,但它有它的局限性。 (请参见 href=\"http://stackoverflow.com/questions/29451126/error13-in-excel-vba-in-data-file\">和的这里

一个简单的功能没有这些限制转其实是很容易建立。你真正需要的是两个阵列和两个嵌套的循环翻转他们。

 子例1()
    昏暗的arrVALs()作为变,arrPHONs()为Variant
    昏暗V作为龙,W只要    随着表(原始数据)。电池(1,1).CurrentRegion
        与.Resize(.Rows.Count - 1,21).Offset(1,0)
            arrVALs = .Cells.Value
            阵列尺寸检查
            'Debug.Print LBOUND(arrVALs,1)及:&放大器; UBound函数(arrVALs,1)
            'Debug.Print LBOUND(arrVALs,2)及:&放大器; UBound函数(arrVALs,2)
            Debug.Print Application.CountIf(.Columns(3),手机)及手机
        结束与
    结束与    使用ReDim arrPHONs(1至UBound函数(arrVALs,2),1比1)
    为V = LBOUND(arrVALs,1)向UBound函数(arrVALs,1)
        如果LCASE(arrVALs(V,3))=手机之后
            为W = LBOUND(arrVALs,2)向UBound函数(arrVALs,2)
                arrPHONs(W,UBound函数(arrPHONs,2))= arrVALs(V,W)
            下一页W
            使用ReDim preserve arrPHONs(1向UBound函数(arrPHONs,1),_
                                    1要UBound函数(arrPHONs,2)+ 1)的
        万一
    下一步v    有1太多的滤波阵列
    使用ReDim preserve arrPHONs(1向UBound函数(arrPHONs,1),_
                            1要UBound函数(arrPHONs,2) - 1)    阵列尺寸检查
    'Debug.Print LBOUND(arrPHONs,1)及:&放大器; UBound函数(arrPHONs,1)
    'Debug.Print LBOUND(arrPHONs,2)及:&放大器; UBound函数(arrPHONs,2)    方法1:使用内置的移调
    。工作表(L)范围(A2:U&放大器; UBound函数(arrPHONs,2)+ 1)= Application.Transpose(arrPHONs)    选项2:使用自定义my_2D_Transpose
    工作表(L)的范围。(A2:U&放大器; UBound函数(arrPHONs,2)+ 1)= my_2D_Transpose(arrPHONs)结束小组功能my_2D_Transpose(ARR为Variant)
    暗淡了长,B长,TMP()为Variant
    使用ReDim TMP(1至UBound函数(ARR,2),1到UBound函数(ARR,1))
    对于= LBOUND(ARR,1)UBound函数(ARR,1)
        对于B = LBOUND(ARR,2)UBound函数(ARR,2)
            TMP(B,A)=修剪(ARR(A,B))
        下一页b
    接下来一个
    my_2D_Transpose TMP =
结束功能

所以,如果你是在赶时间,你的阵列的范围就是这样,你永远达不到 Application.Transpose 的限制然后通过各种手段使用。如果不能安全地使用转则使用自定义的功能。

I have the following code below,

I want to get the entire row not just column 1 of the original array, how would i do this?

Sub Example1()
    Dim arrValues() As Variant
    Dim lastRow As Long
    Dim filteredArray()
    Dim lRow As Long
    Dim lCount As Long
    Dim tempArray()

    lastRow = Sheets("Raw Data").UsedRange.Rows(Sheets("Raw Data").UsedRange.Rows.Count).Row
    arrValues = Sheets("Raw Data").Range(Cells(2, 1), Cells(lastRow, 21)).Value

    ' First use a temporary array with just one dimension
    ReDim tempArray(1 To UBound(arrValues))
    For lCount = 1 To UBound(arrValues)
        If arrValues(lCount, 3) = "phone" Then
            lRow = lRow + 1
            tempArray(lRow) = arrValues(lCount, 1)
        End If
    Next

    ' Now we know how large the filteredArray needs to be: copy the found values into it
    ReDim filteredArray(1 To lRow, 1 To 1)
    For lCount = 1 To lRow
        filteredArray(lCount, 1) = tempArray(lCount)
    Next

    Sheets("L").Range("A2:U" & 1 + lRow) = filteredArray
End Sub

解决方案

The ReDim statement can add records on-the-fly with the PRESERVE parameter but only into the last rank. This is a problem as the second rank of a two dimensioned array is typically considered the 'columns' while the first are the 'rows'.

The Application.Transpose can flip rows into columns and vise-versa but it has limitations. (see here and here)

A simple function to transpose without these limitations is actually very easy to build. All you really need are two arrays and two nested loops to flip them.

Sub Example1()
    Dim arrVALs() As Variant, arrPHONs() As Variant
    Dim v As Long, w As Long

    With Sheets("Raw Data").Cells(1, 1).CurrentRegion
        With .Resize(.Rows.Count - 1, 21).Offset(1, 0)
            arrVALs = .Cells.Value
            'array dimension check
            'Debug.Print LBound(arrVALs, 1) & ":" & UBound(arrVALs, 1)
            'Debug.Print LBound(arrVALs, 2) & ":" & UBound(arrVALs, 2)
            'Debug.Print Application.CountIf(.Columns(3), "phone") & " phones"
        End With
    End With

    ReDim arrPHONs(1 To UBound(arrVALs, 2), 1 To 1)
    For v = LBound(arrVALs, 1) To UBound(arrVALs, 1)
        If LCase(arrVALs(v, 3)) = "phone" Then
            For w = LBound(arrVALs, 2) To UBound(arrVALs, 2)
                arrPHONs(w, UBound(arrPHONs, 2)) = arrVALs(v, w)
            Next w
            ReDim Preserve arrPHONs(1 To UBound(arrPHONs, 1), _
                                    1 To UBound(arrPHONs, 2) + 1)
        End If
    Next v

    'there is 1 too many in the filtered array
    ReDim Preserve arrPHONs(1 To UBound(arrPHONs, 1), _
                            1 To UBound(arrPHONs, 2) - 1)

    'array dimension check
    'Debug.Print LBound(arrPHONs, 1) & ":" & UBound(arrPHONs, 1)
    'Debug.Print LBound(arrPHONs, 2) & ":" & UBound(arrPHONs, 2)

    'Option 1: use built-in Transpose
    'Worksheets("L").Range("A2:U" & UBound(arrPHONs, 2) + 1) = Application.Transpose(arrPHONs)

    'Option 2: use custom my_2D_Transpose
    Worksheets("L").Range("A2:U" & UBound(arrPHONs, 2) + 1) = my_2D_Transpose(arrPHONs)

End Sub

Function my_2D_Transpose(arr As Variant)
    Dim a As Long, b As Long, tmp() As Variant
    ReDim tmp(1 To UBound(arr, 2), 1 To UBound(arr, 1))
    For a = LBound(arr, 1) To UBound(arr, 1)
        For b = LBound(arr, 2) To UBound(arr, 2)
            tmp(b, a) = Trim(arr(a, b))
        Next b
    Next a
    my_2D_Transpose = tmp
End Function

So if you are in a hurry and the scope of your arrays is such that you will never reach the limits of Application.Transpose then by all means use it. If you cannot safely use transpose then use a custom function.

这篇关于得到数组的整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆