【问题标题】:Is there a way to fill in cell in excel based on array "x" number of times有没有办法根据数组“x”的次数在excel中填写单元格
【发布时间】:2020-11-18 21:50:37
【问题描述】:

我正在尝试创建一个 VBA 脚本来根据数组填充 Excel 中的单元格 X 次。 目标是填充第一个数组值 50 次,然后填充下一个数组值 50 次,依此类推。 目前,我设法使用以下代码使其工作,但它只填充一个单元格,并且将进入下一个单元格。 有人知道如何更新此代码并使其用“55”填充 50 个单元格,然后用“66”填充 50 个单元格吗? 提前致谢!


Sub StaticArrayTest()
    Dim Myarray(1 To 3) As Integer
    Dim i As Integer
    
    Myarray(1) = 55
    Myarray(2) = 66
    Myarray(3) = 77
    
    For i = 1 To UBound(Myarray)
        Cells(i, 1).Value = Myarray(i)
    Next i

End Sub

【问题讨论】:

    标签: excel vba loops foreach


    【解决方案1】:

    有趣的小选择:

    Sub Test()  
        Range("A1:A150").Value = [CHOOSE(ROUNDUP(ROW(1:150)/50,0),55,66,77)]
    End Sub
    

    注意:不要被这篇文章的简短所迷惑。它可能被证明更难维护并且运行时间更长。只是抛出另一个选项=)

    【讨论】:

      【解决方案2】:

      这应该可以解决问题。您需要更多变量来跟踪单元格的位置并循环遍历每个值:

      Option Explicit
      Sub StaticArrayTest()
      
          Dim Myarray(1 To 3) As Long 'Never use integer in Excel
          Myarray(1) = 55
          Myarray(2) = 66
          Myarray(3) = 77
          
          Dim NumberOfIterations As Long: NumberOfIterations = 50
          Dim i As Long, j As Long
          Dim Position As Long: Position = 1
          For i = 1 To UBound(Myarray)
              For j = 1 To NumberOfIterations
                  Cells(Position, 1) = Myarray(i)
                  Position = Position + 1
              Next j
          Next i
      
      End Sub
      

      【讨论】:

        【解决方案3】:

        我的做法会稍有不同。

        我会创建一个新数组来保存所有值,然后将数组写入 ONE GO 中的工作表。像这样的东西。它还使您可以灵活地轻松更改值。

        Option Explicit
        
        Sub StaticArrayTest()
            Dim Myarray(1 To 3) As Long
            Myarray(1) = 55
            Myarray(2) = 66
            Myarray(3) = 77
            
            '~~> Number of iterations
            Dim slots As Long
            slots = 50
            
            '~~> Final array to hold values
            Dim finalArray() As Long
            ReDim finalArray(1 To slots * UBound(Myarray))
            
            Dim i As Long, j As Long, k As Long
            k = 1
            
            '~~> Populate the array
            For i = LBound(Myarray) To UBound(Myarray)
                For j = 1 To slots
                    finalArray(k) = Myarray(i)
                    k = k + 1
                Next j
            Next i
            
            Dim ws As Worksheet
            Set ws = Sheet1
            
            '~~> Write to the sheet in 1 go
            ws.Range("A1").Resize(UBound(finalArray), 1).Value = _
            WorksheetFunction.Transpose(finalArray)
        End Sub
        

        【讨论】:

          【解决方案4】:

          更接近 OP 的方法...

          您的主要问题是您通过仅填充一个行范围来遍历三个数组项;用给定的偏移量定义一个开始范围将会成功并且可以减少循环周期:

          Sub StaticArrayTestMe()
              '~~> Number of iterations
              Const slots As Long = 50
              '~~> values to insert repeatedly
              Dim Myarray(1 To 3) As Long
              Myarray(1) = 55
              Myarray(2) = 66
              Myarray(3) = 77
              
              With Sheet1.Range("A1:A" & slots)            ' refer to start range of 50 cells using the sheet's Code(Name)
                  Dim i As Long
                  For i = LBound(Myarray) To UBound(Myarray) ' here: 1st item to 3rd item
                      'calculate new row offsets to start range: (1) 0, (2) 50, (3) 100
                      .Offset((i - 1) * slots) = Myarray(i)  ' fill in 1st,2nd & 3rd item
                  Next i
              End With
          End Sub
          

          【讨论】:

            猜你喜欢
            • 2011-04-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-04
            相关资源
            最近更新 更多