【问题标题】:Add column (as first) with 1 to exsiting Variant Array in VBA将带有 1 的列(作为第一列)添加到 VBA 中的现有 Variant Array
【发布时间】:2016-12-08 22:51:29
【问题描述】:

我有一个包含 1 列或更多列的数组,现在我想再添加一列(仅包含 1 列),但我不知道该怎么做。情况是这样的:

我的代码:

Dim X() As Variant
X = Range("A1:C3").Value2

将 1 列放在首位很重要。可能我需要使用 ReDim Preserve 但对我没有用。

【问题讨论】:

  • 你以后用数组做什么? 1 纯粹用于处理目的,还是您最终需要将其写回工作表?
  • 只是为了计算
  • 基于1 的数组的美妙之处在于,您可以随意处理0 的索引。只需对其进行硬编码,而不是从数组中访问它。
  • 好吧,我解决了,你建议的其他方式,但它有效! Ty 回复。
  • @Roberto - 仅供参考,您的问题是如何使用Application.Index() 的一些相对未知的特性将列插入到变体数组中。 - 希望得到反馈;随时接受/投票。

标签: arrays excel vba multidimensional-array


【解决方案1】:

我认为您有一些选择,但不是扩展数组的索引和转置,尝试移动值等似乎太麻烦了,我宁愿将 1 添加到 Excel 范围,然后创建数组:

Range("B1:D3").Value2 = Range("A1:C3").Value2
Range("A1:A3").Value2 = 1
X = Range("A1:D3").Value2

【讨论】:

    【解决方案2】:
    • 调整数组大小,在最后一维中添加一列
    • 将所有数据右移。
    • 将 1 分配给每行的第一个位置

    Sub AddColumnShiftData()
    
        Dim X As Variant
        Dim i As Long, j As Long
        X = Range("A1:C3").Value2
        ReDim Preserve X(1 To 3, 1 To 4)
    
        For i = 1 To UBound(X)
            For j = UBound(X, 2) To 2 Step -1
                X(i, j) = X(i, j - 1)
            Next
            X(i, 1) = 1
        Next
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      尝试用识别矩阵进行矩阵乘法......好吧,几乎是单位矩阵。然后将结果矩阵中的每个元素加 1。您可以使用 Excel 的 Worksheet 函数进行矩阵乘法。

      Almost identity matrix

      Dim X As Variant
      X = Range("A1:C3").Value2
      Dim Y As Variant
      n = UBound(X, 2)
      m = n + 1
      Z = UBound(X, 1)
      ReDim Y(1 To n, 1 To m)
      
      
      'Set All values to zero
      For i = 1 To n
          For j = 1 To m
              Y(i, j) = 0
          Next j
      Next i
      
      ' Set  offset diagonal to 1
      For i = 1 To n
          Y(i, i + 1) = 1
      Next i
      
      ' Matrix MMult
      X = Application.WorksheetFunction.MMult(X, Y)
      ' Add 1 to the first column
      For i = 1 To Z
          X(i, 1) = 1
      Next i
      

      【讨论】:

        【解决方案4】:

        通过Application.Index()替代

        只是为了好玩(注意结果数组是一个基于 1 的 2-dim 数组):

        Sub AddFirstIndexColumn()
            Const FIXEDVALUE = 1         ' value to replace in new column 1
            '[1] get data
            Dim v: v = getExampleData()
        
            '[2] define column array inserting first column (0 or 1) and preserving old values (1,2,3)
            v = Application.Index(v, _
                                  Application.Evaluate("row(1:" & UBound(v) & ")"), _
                                  Array(1, 1, 2, 3))     ' columns array where 0 reinserts the first column
            ' [3] add an current number in the first column
            Dim i As Long
            For i = LBound(v) To UBound(v): v(i, 1) = FIXEDVALUE: Next i
        End Sub
        
        Function getExampleData()
        ' Method: just for fun a rather unusual way to create a 2-dim array
        ' Caveat: time-consuming for greater data sets; better to assign a range to a datafield array
        Dim v
            v = Array(Array(2, 3, 5), Array(3, 8, 9), Array(4, 2, 1))
            v = Application.Index(v, 0, 0)
        getExampleData = v
        End Function
        

        相关链接

        Some pecularities of `Application.Index()

        Insert vertical slices into array

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-28
          • 2022-01-19
          • 2023-04-06
          • 2019-03-24
          相关资源
          最近更新 更多