Ubound Lbound,数组上下边界 将二维数组处理后写入新数组,并粘贴到单元格中。

    xiaoxiao2025-08-23  3

    Sub k()

    Dim arr, arr2 arr = [b1:c19] ‘定义操作数组为b1到c19的区域’ icount = Application.CountIf([b:b], "A") ‘icount变量定义为b列中出现的A的个数’ ReDim arr2(1 To icount, 1 To 2) ‘定义需要写入的数组,为2维数组,第一维(行,该维度取决于A的个数)为从1到icount,第二维为2(列)’ For i = 1 To UBound(arr) ‘对于i从1到arr的最大的维度数:Ubound(数组)返回该数组的第一维的最大个数’     If arr(i, 1) = "A" Then ‘如果arr的i行1列为A’         n = n + 1         arr2(n, 1) = arr(i, 1)‘将arr的i行1列写入arr2的n行1列’         arr2(n, 2) = arr(i, 2)‘将arr的i行2列写入arr2的n行2列’     End If Next [d1].Resize(icount, 2) = arr2‘将d1单元格重置大小,并写入数组arr2’

    End Sub

    Ubound(数组)返回一个数组的第n维的最大下标,举个栗子

    arr(1 to 5,2 to 10)是一个二维数组

    msgbox Ubound(arr)返回5

    msgbox Ubound (arr,2)返回10

    msgbox Ubound (arr,1) 返回5

    msgbox Lbound (arr,1)返回1 Lbound 返回最小的标号

    转载请注明原文地址: https://ju.6miu.com/read-1301959.html
    最新回复(0)